Class: Reaction::Action
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#attributes, included, #set_attributes, #validate_attributes
Constructor Details
#initialize(param_values = {}, attribute_values = {}) ⇒ Action
Returns a new instance of Action.
9
10
11
12
|
# File 'lib/reaction/action.rb', line 9
def initialize(param_values = {}, attribute_values = {})
set_params(param_values)
set_attributes(attribute_values)
end
|
Instance Attribute Details
#error ⇒ Object
Returns the value of attribute error.
7
8
9
|
# File 'lib/reaction/action.rb', line 7
def error
@error
end
|
#result ⇒ Object
Returns the value of attribute result.
6
7
8
|
# File 'lib/reaction/action.rb', line 6
def result
@result
end
|
#successful ⇒ Object
Returns the value of attribute successful.
5
6
7
|
# File 'lib/reaction/action.rb', line 5
def successful
@successful
end
|
Class Method Details
.param(name, type = RawType, options = {}) ⇒ Object
88
89
90
|
# File 'lib/reaction/action.rb', line 88
def self.param(name, type = RawType, options = {})
param_builder(name).type_builder = TypeBuilder.new(type, options)
end
|
.param_builder(name) ⇒ Object
83
84
85
86
|
# File 'lib/reaction/action.rb', line 83
def self.param_builder(name)
name.to_sym
param_builders[name] ||= ParamBuilder.new(name)
end
|
.param_builders ⇒ Object
79
80
81
|
# File 'lib/reaction/action.rb', line 79
def self.param_builders
@param_builders ||= {}
end
|
.process(param_values = {}, attribute_values = {}) ⇒ Object
70
71
72
73
74
75
76
77
|
# File 'lib/reaction/action.rb', line 70
def self.process(param_values = {}, attribute_values = {})
action = new(param_values, attribute_values)
if action.process
return [action.result, nil]
else
return [nil, action.error]
end
end
|
.validates(name, validators = {}) ⇒ Object
92
93
94
95
96
|
# File 'lib/reaction/action.rb', line 92
def self.validates(name, validators = {})
validators.each do |validator, options|
param_builder(name).validator_builders << ValidatorBuilder.new(validator, options)
end
end
|
Instance Method Details
#failure(error) ⇒ Object
30
31
32
33
|
# File 'lib/reaction/action.rb', line 30
def failure(error)
@successful = false
@error = error
end
|
#param(name, mode = :converted) ⇒ Object
This is how you retrieve a param. Pass in a mode of :raw in order to get the raw param if you happen to need that, but in general you shouldn’t.
39
40
41
42
43
44
45
|
# File 'lib/reaction/action.rb', line 39
def param(name, mode = :converted)
if mode == :raw
_param(name).raw_value
else
_param(name).result
end
end
|
#param_provided?(name) ⇒ Boolean
66
67
68
|
# File 'lib/reaction/action.rb', line 66
def param_provided?(name)
_param(name).provided?
end
|
#params(mode = :converted) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/reaction/action.rb', line 47
def params(mode = :converted)
ret = {}
_params.each do |name, param|
if mode == :raw
ret[name] = param.raw_value if param_provided?
else
ret[name] = param.result if param.provided?
end
end
ret
end
|
#process ⇒ Object
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/reaction/action.rb', line 14
def process
return false unless validate_attributes
_params.each do |name, param|
unless param.process
failure(param.error) and return false
end
end
perform
return !!@successful
end
|
#set_params(param_values = {}) ⇒ Object
59
60
61
62
63
64
|
# File 'lib/reaction/action.rb', line 59
def set_params(param_values = {})
self.class.param_builders.each do |name, pb|
provided = param_values.keys.include?(name)
_params[name] = pb.build(self, param_values[name], provided)
end
end
|
#success(result) ⇒ Object
25
26
27
28
|
# File 'lib/reaction/action.rb', line 25
def success(result)
@successful = true
@result = result
end
|