-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_base.rb
77 lines (56 loc) · 1.49 KB
/
service_base.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
require_relative "command"
require_relative "Rules/greater_than_integer_rule"
class ServiceBase
def initialize(data_proxy)
@data_proxy = data_proxy
end
def get_by_id_command(id)
Command.new(Proc.new { get_by_id(id) }, Proc.new { rules_for_get_by_id(id) })
end
def get_all_command
Command.new(Proc.new { get_all }, Proc.new { rules_for_get_all })
end
def insert_command(entity)
Command.new(Proc.new { insert(entity) }, Proc.new { rules_for_insert(entity) })
end
def update_command(entity)
Command.new(Proc.new { update(entity) }, Proc.new { rules_for_update(entity) })
end
def delete_command(id)
Command.new(Proc.new { delete(id) }, Proc.new { rules_for_delete(id) })
end
protected
def get_by_id(id)
@data_proxy.get_by_id(id)
end
def get_all
@data_proxy.get_all
end
def insert(entity)
@data_proxy.insert(entity)
end
def update(entity)
#current = get_by_id(entity[:id])
#current.revert_non_editable_fields()
@data_proxy.update(entity)
end
def delete(id)
@data_proxy.delete(id)
end
def rules_for_get_by_id(id)
[ GreaterThanIntegerRule.new(:id, id, 0) ]
end
def rules_for_get_all
[]
end
def rules_for_insert(entity)
[]
end
def rules_for_update(entity)
[ FieldRequiredRule.new(entity, :id)
.if_valid_then_validate(GreaterThanIntegerRule.new(:id, entity[:id], 0)) ]
end
def rules_for_delete(id)
[ GreaterThanIntegerRule.new(:id, id, 0) ]
end
end