Class: Bricolage::KeyValuePairsParam
- Inherits:
-
Param
- Object
- Param
- Bricolage::KeyValuePairsParam
show all
- Defined in:
- lib/bricolage/parameters.rb
Instance Attribute Summary
Attributes inherited from Param
#arg_spec, #description, #name
Instance Method Summary
collapse
Methods inherited from Param
#have_arg?, #inspect, #option_name, #optional?, #publish?, #required?
Constructor Details
#initialize(name, arg_spec, description, optional: true, default: nil, value_handler: nil) ⇒ KeyValuePairsParam
Returns a new instance of KeyValuePairsParam.
635
636
637
638
639
|
# File 'lib/bricolage/parameters.rb', line 635
def initialize(name, arg_spec, description, optional: true, default: nil, value_handler: nil)
super name, arg_spec, description, optional: optional, publish: false
@default_value = default
@value_handler = value_handler
end
|
Instance Method Details
#default_value(ctx, vars) ⇒ Object
664
665
666
|
# File 'lib/bricolage/parameters.rb', line 664
def default_value(ctx, vars)
@default_value
end
|
#materialize(pairs, ctx, vars) ⇒ Object
668
669
670
671
672
673
674
675
676
677
678
679
680
681
|
# File 'lib/bricolage/parameters.rb', line 668
def materialize(pairs, ctx, vars)
if @value_handler
@value_handler.call(pairs, ctx, vars)
else
unless pairs.kind_of?(Hash)
raise "[BUG] bad value type #{pairs.class} for KeyValuePairsParam\#materialize (#{name})"
end
h = {}
pairs.each do |name, value|
h[name] = expand(value.to_s, vars)
end
h
end
end
|
#parse_option_value(value, h) ⇒ Object
641
642
643
644
645
646
647
648
|
# File 'lib/bricolage/parameters.rb', line 641
def parse_option_value(value, h)
var, spec = value.split(':', 2)
if not var or var.empty?
raise ParameterError, "missing variable name: #{value.inspect}"
end
(h ||= {})[var] = spec
h
end
|
#parse_value(h) ⇒ Object
650
651
652
653
654
655
656
657
658
659
660
661
662
|
# File 'lib/bricolage/parameters.rb', line 650
def parse_value(h)
case h
when Hash
h.empty? ? nil : h
when String
raise ParameterError, "bad type for parameter #{name}: #{h.class}" unless @value_handler
h.strip.empty? ? nil : h
when nil, false
{}
else
raise ParameterError, "bad type for parameter #{name}: #{h.class}"
end
end
|
#variables(h) ⇒ Object
683
684
685
|
# File 'lib/bricolage/parameters.rb', line 683
def variables(h)
[]
end
|