lib/metaractor/parameters.rb in metaractor-3.3.0 vs lib/metaractor/parameters.rb in metaractor-3.3.2
- old
+ new
@@ -29,11 +29,11 @@
end
def <=>(other)
return nil unless other.instance_of? self.class
return nil if name == other.name && options != other.options
- self.name.to_s <=> other.name.to_s
+ name.to_s <=> other.name.to_s
end
def [](key)
@options[key]
end
@@ -57,20 +57,21 @@
def to_sym
name
end
protected
+
attr_reader :options
end
module ClassMethods
def parameter(name, **options)
- if param = self.parameter_hash[name.to_sym]
+ if (param = parameter_hash[name.to_sym])
param.merge!(**options)
else
Parameter.new(name, **options).tap do |parameter|
- self.parameter_hash[parameter.name] = parameter
+ parameter_hash[parameter.name] = parameter
end
end
end
def parameters(*names, **options)
@@ -88,11 +89,11 @@
end
def required(*params, **options)
if params.empty?
tree = options
- self.requirement_trees << tree
+ requirement_trees << tree
parameters(*parameters_in_tree(tree), required: tree)
else
parameters(*params, required: true, **options)
end
end
@@ -101,20 +102,20 @@
parameters(*params, **options)
end
def validate_parameters(*hooks, &block)
hooks << block if block
- hooks.each {|hook| validate_hooks.push(hook) }
+ hooks.each { |hook| validate_hooks.push(hook) }
end
def validate_hooks
@validate_hooks ||= []
end
def parameters_in_tree(tree)
if tree.respond_to?(:to_h)
- tree.to_h.values.first.to_a.flat_map {|t| parameters_in_tree(t)}
+ tree.to_h.values.first.to_a.flat_map { |t| parameters_in_tree(t) }
else
[tree]
end
end
end
@@ -126,11 +127,11 @@
def requirement_trees
self.class.requirement_trees
end
def requirement_trees=(trees)
- self.class.requirement_trees=(trees)
+ self.class.requirement_trees = (trees)
end
def remove_blank_values
to_delete = []
context.each_pair do |name, value|
@@ -154,24 +155,26 @@
end
end
end
def _parameter_default(name)
- default = self.parameters[name][:default]
+ default = parameters[name][:default]
- case
- when default.respond_to?(:call) then instance_exec(&default)
- when default.respond_to?(:dup) then default.dup
- else default
+ if default.respond_to?(:call)
+ instance_exec(&default)
+ elsif default.respond_to?(:dup)
+ default.dup
+ else
+ default
end
end
def apply_types
parameters.each do |name, parameter|
next unless parameter[:type]
- if context.has_key?(name) && context[name] != nil
+ 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?
@@ -204,11 +207,11 @@
def require_parameter(param, message: nil)
message_override = message
valid, message = parameter_valid? param
if !valid
- if message_override != nil
+ if !message_override.nil?
add_parameter_error(param: param, message: message_override)
else
add_parameter_error(message: "Required parameters: #{message}")
end
end
@@ -232,20 +235,20 @@
messages << message
end
case operator
when :or
- return valids.any?, "(#{messages.join(' or ')})"
+ [valids.any?, "(#{messages.join(" or ")})"]
when :xor
- return valids.one?, "(#{messages.join(' xor ')})"
+ [valids.one?, "(#{messages.join(" xor ")})"]
when :and
- return valids.all?, "(#{messages.join(' and ')})"
+ [valids.all?, "(#{messages.join(" and ")})"]
else
raise "invalid required parameter #{param.inspect}"
end
else
- return context[param] != nil, param.to_s
+ [!context[param].nil?, param.to_s]
end
end
def require_parameter!(param, message: nil)
require_parameter param, message: message
@@ -254,10 +257,10 @@
def run_validate_hooks
run_hooks(self.class.validate_hooks)
end
- def add_parameter_error(param: nil, message:)
+ def add_parameter_error(message:, param: nil)
add_error(
message: "#{param} #{message}".lstrip
)
context.invalidate!