Class: PaymentRecipes::Utils::Action
- Inherits:
-
Object
- Object
- PaymentRecipes::Utils::Action
show all
- Defined in:
- lib/payment_recipes/utils/action.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(parameters = {}) ⇒ Action
Returns a new instance of Action.
53
54
55
56
57
58
59
60
|
# File 'lib/payment_recipes/utils/action.rb', line 53
def initialize(parameters = {})
raise TypeError, "Action params should be a Hash" unless parameters.is_a?(Hash)
@params = parameters
@params.each do |param, value|
instance_variable_set("@#{ param }".to_sym, value)
end
end
|
Class Method Details
.debug ⇒ Object
48
49
50
|
# File 'lib/payment_recipes/utils/action.rb', line 48
def debug
study rules
end
|
.prepare(parameters = {}) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/payment_recipes/utils/action.rb', line 33
def prepare(parameters = {})
rules.each do |key, klass|
allowed_class_strings = parameters[key].class.ancestors.map(&:to_s)
unless allowed_class_strings.include?(klass) || allowed_class_strings.include?("NilClass")
raise TypeError, "#{ key } should be a #{ klass }"
end
end
output = new(parameters)
output.instance_variable_set(:@_action_state, :prepared)
output
end
|
.rules ⇒ Object
29
30
31
|
# File 'lib/payment_recipes/utils/action.rb', line 29
def rules
@rules || []
end
|
.variable(key, klass) ⇒ Object
20
21
22
23
24
25
26
27
|
# File 'lib/payment_recipes/utils/action.rb', line 20
def variable(key, klass)
@rules ||= {}
@rules[key] = klass
define_method key do
instance_variable_get("@#{ key }".to_sym)
end
end
|
Instance Method Details
#debug ⇒ Object
171
172
173
|
# File 'lib/payment_recipes/utils/action.rb', line 171
def debug
study @params
end
|
#ensure_presence(variable, default: nil) ⇒ Object
119
120
121
122
123
124
125
|
# File 'lib/payment_recipes/utils/action.rb', line 119
def ensure_presence(variable, default: nil)
if variable.present?
yield if block_given?
else
default
end
end
|
#execute ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/payment_recipes/utils/action.rb', line 70
def execute
@success = nil
@error = nil
unless @_action_state
raise Exception, "Use #{ self.class.name }.prepare to initialize this action"
end
unless @_action_state == :executed
@_action_output = perform
if @error
@success = false
else
@success = true
end
make_instance_variables_available
@_action_state = :executed
end
@_action_output
end
|
#failure? ⇒ Boolean
66
67
68
|
# File 'lib/payment_recipes/utils/action.rb', line 66
def failure?
not success?
end
|
#get(label) ⇒ Object
165
166
167
168
169
|
# File 'lib/payment_recipes/utils/action.rb', line 165
def get(label)
label_sym = "@#{label}".to_sym
instance_variable_get(label_sym)
end
|
#make_instance_variables_available ⇒ Object
111
112
113
114
115
116
117
|
# File 'lib/payment_recipes/utils/action.rb', line 111
def make_instance_variables_available
instance_variables.each do |instance_variable|
define_singleton_method(instance_variable.to_s.gsub("@", "")) do
instance_variable_get(instance_variable)
end
end
end
|
#set(label, value) ⇒ Object
153
154
155
156
157
158
159
160
161
162
163
|
# File 'lib/payment_recipes/utils/action.rb', line 153
def set(label, value)
label_sym = "@#{label}".to_sym
instance_variable_set(label_sym, value)
define_singleton_method(label_sym.to_s.gsub("@", "")) do
instance_variable_get(label_sym)
end
value
end
|
#store(label) ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/payment_recipes/utils/action.rb', line 133
def store(label)
label_sym = "@#{label}".to_sym
if stored_value = instance_variable_get(label_sym)
stored_value
else
if block_given?
computed_value = yield
instance_variable_set(label_sym, computed_value)
define_singleton_method(label_sym.to_s.gsub("@", "")) do
instance_variable_get(label_sym)
end
end
end
instance_variable_get(label_sym)
end
|
#stored_variables ⇒ Object
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/payment_recipes/utils/action.rb', line 95
def stored_variables
output = {}
instance_variables.each do |instance_variable|
instance_variable_sym = instance_variable.to_s.gsub("@", "").to_sym
begin
output[instance_variable_sym] = send(instance_variable_sym)
rescue Exception => e
end
end
output
end
|
#success? ⇒ Boolean
62
63
64
|
# File 'lib/payment_recipes/utils/action.rb', line 62
def success?
@success
end
|