Class: Bricolage::KeyValuePairsParam

Inherits:
Param
  • Object
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.



639
640
641
642
643
# File 'lib/bricolage/parameters.rb', line 639

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



668
669
670
# File 'lib/bricolage/parameters.rb', line 668

def default_value(ctx, vars)
  @default_value
end

#materialize(pairs, ctx, vars) ⇒ Object



672
673
674
675
676
677
678
679
680
681
682
683
684
685
# File 'lib/bricolage/parameters.rb', line 672

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



645
646
647
648
649
650
651
652
# File 'lib/bricolage/parameters.rb', line 645

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   # accumulator
end

#parse_value(h) ⇒ Object



654
655
656
657
658
659
660
661
662
663
664
665
666
# File 'lib/bricolage/parameters.rb', line 654

def parse_value(h)
  case h
  when Hash
    h.empty? ? nil : h
  when String   # FIXME: should be removed after changing all load options
    raise ParameterError, "bad type for parameter #{name}: #{h.class}" unless @value_handler
    h.strip.empty? ? nil : h
  when nil, false   # disables option explicitly
    {}
  else
    raise ParameterError, "bad type for parameter #{name}: #{h.class}"
  end
end

#variables(h) ⇒ Object



687
688
689
# File 'lib/bricolage/parameters.rb', line 687

def variables(h)
  []
end