Skip to content

Commit d2e1b1e

Browse files
author
ahanusa
committed
rules engine refactor
1 parent c661e6f commit d2e1b1e

File tree

5 files changed

+58
-29
lines changed

5 files changed

+58
-29
lines changed

Rules/field_length_rule.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ def initialize(entity, field, length, display="")
99
end
1010

1111
def on_validate
12-
@is_valid = @entity[@field].length <= @length
13-
@error_message = "#{@field} must be equal to or less than #{@length} characters"
14-
@is_valid
12+
if @entity[@field].length > @length
13+
invalidate("#{@field} must be equal to or less than #{@length} characters")
14+
end
1515
end
1616
end

Rules/field_required_rule.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require_relative '../rule_base'
2+
3+
class FieldRequiredRule < RuleBase
4+
def initialize(entity, field)
5+
@entity = entity
6+
@field = field
7+
end
8+
9+
def on_validate
10+
if @entity[@field].to_s.empty?
11+
invalidate("#{@field} is a required field")
12+
end
13+
end
14+
end

command_base.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def execute
2525

2626
def get_validation_errors
2727
rules = @rules_method.call
28-
errors = rules.select { |rule| rule.validate; !rule.is_valid }
28+
errors = rules.select { |rule| rule.validate; !rule.valid? }
2929
.map { |error| error.error_message }
3030
end
3131

rule_base.rb

+32-21
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,58 @@
11
class RuleBase
2-
attr_reader :is_valid,
3-
:error_message,
2+
attr_reader :error_message,
43
:successor,
54
:if_valid_then_execute, #The action to perform once when this rule passes validation.
65
:if_invalid_then_execute #The action to perform once when this rule fails validation.
76

87
def validate
8+
@valid = true
99
on_validate
10-
if is_valid
10+
if valid?
1111
if successor
12-
successor.validate()
13-
is_valid = successor.is_valid
14-
error_message = successor.error_message
12+
successor.validate
13+
invalidate(successor.error_message) if !successor.valid?
14+
end
15+
if @if_valid_then_execute
16+
@if_valid_then_execute.call(self)
17+
@if_valid_then_execute = nil
18+
end
19+
else
20+
if @if_invalid_then_execute
21+
@if_invalid_then_execute.call(self)
22+
@if_invalid_then_execute = nil
1523
end
16-
#if @if_valid_then_execute
17-
#@if_valid_then_execute.call(self)
18-
#@if_valid_then_execute = nil
19-
#end
20-
#else
21-
#if @if_invalid_then_execute
22-
#@if_invalid_then_execute.call(self)
23-
#@if_invalid_then_execute = nil
24-
#end
2524
end
2625
return self
2726
end
2827

28+
def valid?
29+
@valid
30+
end
31+
2932
def if_valid_then_validate(rule)
3033
@successor = rule
34+
self
3135
end
3236

33-
#def if_valid_then_execute(&method)
34-
#@if_valid_then_execute = method
35-
#end
37+
def if_valid_then_execute(method)
38+
@if_valid_then_execute = method
39+
self
40+
end
3641

37-
#def if_invalid_then_execute(&method)
38-
#@if_invalid_then_execute = method
39-
#end
42+
def if_invalid_then_execute(method)
43+
@if_invalid_then_execute = method
44+
self
45+
end
4046

4147
protected
4248

4349
def on_validate
4450
raise "on_validate must be implemented"
4551
end
4652

53+
def invalidate(message)
54+
@error_message = message
55+
@valid = false
56+
end
57+
4758
end

tests.rb

+8-4
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@
44
require_relative "DataProxies/DatabaseProxies/customer_database_proxy"
55
require_relative "DataProxies/RestfulServiceProxies/customer_restful_proxy"
66
require_relative "Rules/field_length_rule"
7+
require_relative "Rules/field_required_rule"
78

89
class CustomerService < ServiceBase
910
def initialize(data_proxy)
1011
super(data_proxy)
1112
end
1213
def rules_for_insert(entity)
1314
rules = []
14-
rules << FieldLengthRule.new(entity, :name, 10)
15+
rules << FieldRequiredRule.new(entity, :name)
16+
.if_valid_then_execute(lambda { |rule| puts "WOOT!" })
17+
.if_valid_then_validate(FieldLengthRule.new(entity, :name, 10)
18+
.if_invalid_then_execute(lambda { |rule| puts "ARGH!!" }))
1519
rules
1620
end
1721
end
1822

19-
customer_data_proxy = CustomerRestfulProxy.new
20-
#customer_data_proxy = CustomerDatabaseProxy.new
23+
#customer_data_proxy = CustomerRestfulProxy.new
24+
customer_data_proxy = CustomerDatabaseProxy.new
2125

2226
service = CustomerService.new(customer_data_proxy)
23-
result = service.insert_command({:name => "AaronHanusaa"}).execute
27+
result = service.insert_command({:name => "AaronHanus"}).execute
2428
puts result.inspect
2529

2630
result = service.get_by_id_command(1).execute

0 commit comments

Comments
 (0)