lib/meta/utils/kwargs/builder.rb in meta-api-0.0.6 vs lib/meta/utils/kwargs/builder.rb in meta-api-0.0.7
- old
+ new
@@ -53,14 +53,15 @@
end
class Argument
DEFAULT_TRANSFORMER = ->(value) { value }
- def initialize(name:, normalizer: DEFAULT_TRANSFORMER, alias_names: [])
+ def initialize(name:, normalizer: DEFAULT_TRANSFORMER, validator: nil, default: nil, alias_names: [])
@key_name = name
@consumer_names = [name] + alias_names
- @normalizer = normalizer
+ @normalizer = default ? ->(value) { normalizer.call(value || default) } : normalizer
+ @validator = validator
end
def consume(final_args, args)
@consumer_names.each do |name|
return true if consume_name(final_args, args, name)
@@ -69,9 +70,10 @@
end
def consume_name(final_args, args, consumer_name)
if args.key?(consumer_name)
value = @normalizer.call(args.delete(consumer_name))
+ @validator.call(value) if @validator
final_args[@key_name] = value
true
else
false
end