Module: Metaractor::Parameters
- Defined in:
- lib/metaractor/parameters.rb
Defined Under Namespace
Modules: ClassMethods
Classes: Parameter
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.included(base) ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/metaractor/parameters.rb', line 5
def self.included(base)
base.extend ClassMethods
base.class_eval do
include Metaractor::HandleErrors
class << self
attr_writer :requirement_trees
end
before :remove_blank_values
before :apply_defaults
before :validate_required_parameters
before :apply_types
end
end
|
Instance Method Details
#_parameter_default(name) ⇒ Object
159
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/metaractor/parameters.rb', line 159
def _parameter_default(name)
default = parameters[name][:default]
if default.respond_to?(:call)
instance_exec(&default)
elsif default.respond_to?(:dup)
default.dup
else
default
end
end
|
#add_parameter_error(message:, param: nil) ⇒ Object
262
263
264
265
266
267
268
|
# File 'lib/metaractor/parameters.rb', line 262
def add_parameter_error(message:, param: nil)
add_error(
message: "#{param} #{message}".lstrip
)
context.invalidate!
end
|
#apply_defaults ⇒ Object
149
150
151
152
153
154
155
156
157
|
# File 'lib/metaractor/parameters.rb', line 149
def apply_defaults
parameters.each do |name, parameter|
next unless parameter.has_key?(:default)
unless context.has_key?(name)
context[name] = _parameter_default(name)
end
end
end
|
#apply_types ⇒ Object
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
# File 'lib/metaractor/parameters.rb', line 171
def apply_types
parameters.each do |name, parameter|
next unless parameter[:type]
if context.has_key?(name) && !context[name].nil?
callable = parameter[:type]
if callable.is_a?(Symbol)
callable = Metaractor.types[callable]
raise ArgumentError, "No such type: #{parameter[:type]}" if callable.nil?
end
context[name] = callable.call(context[name])
end
end
end
|
#parameter_valid?(param) ⇒ Boolean
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
# File 'lib/metaractor/parameters.rb', line 220
def parameter_valid?(param)
if param.respond_to?(:to_h)
param = param.to_h
raise "invalid required parameter #{param.inspect}" unless param.keys.size == 1
raise "invalid required parameter #{param.inspect}" unless param.values.first.respond_to?(:to_a)
operator = param.keys.first
params = param.values.first.to_a
valids = []
messages = []
params.each do |key|
valid, message = parameter_valid?(key)
valids << valid
messages << message
end
case operator
when :or
[valids.any?, "(#{messages.join(" or ")})"]
when :xor
[valids.one?, "(#{messages.join(" xor ")})"]
when :and
[valids.all?, "(#{messages.join(" and ")})"]
else
raise "invalid required parameter #{param.inspect}"
end
else
[!context[param].nil?, param.to_s]
end
end
|
#parameters ⇒ Object
123
124
125
|
# File 'lib/metaractor/parameters.rb', line 123
def parameters
self.class.parameter_hash
end
|
#remove_blank_values ⇒ Object
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/metaractor/parameters.rb', line 135
def remove_blank_values
to_delete = []
context.each_pair do |name, value|
next if parameters.dig(name, :allow_blank)
to_delete << name if (value.is_a?(String) && /\A[[:space:]]*\z/ === value) || value.nil?
end
to_delete.each do |name|
context.delete_field name
end
end
|
#require_parameter(param, message: nil) ⇒ Object
207
208
209
210
211
212
213
214
215
216
217
218
|
# File 'lib/metaractor/parameters.rb', line 207
def require_parameter(param, message: nil)
message_override = message
valid, message = parameter_valid? param
if !valid
if !message_override.nil?
add_parameter_error(param: param, message: message_override)
else
add_parameter_error(message: "Required parameters: #{message}")
end
end
end
|
#require_parameter!(param, message: nil) ⇒ Object
253
254
255
256
|
# File 'lib/metaractor/parameters.rb', line 253
def require_parameter!(param, message: nil)
require_parameter param, message: message
context.fail! unless context.errors.empty?
end
|
#requirement_trees ⇒ Object
127
128
129
|
# File 'lib/metaractor/parameters.rb', line 127
def requirement_trees
self.class.requirement_trees
end
|
#requirement_trees=(trees) ⇒ Object
131
132
133
|
# File 'lib/metaractor/parameters.rb', line 131
def requirement_trees=(trees)
self.class.requirement_trees = (trees)
end
|
#run_validate_hooks ⇒ Object
258
259
260
|
# File 'lib/metaractor/parameters.rb', line 258
def run_validate_hooks
run_hooks(self.class.validate_hooks)
end
|
#validate_required_parameters ⇒ Object
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
# File 'lib/metaractor/parameters.rb', line 188
def validate_required_parameters
context.errors ||= []
parameters.each do |name, parameter|
next if !parameter[:required] ||
parameter[:required].is_a?(Hash)
require_parameter name
end
requirement_trees.each do |tree|
require_parameter tree
end
run_validate_hooks
context.fail! unless context.errors.empty?
end
|