Class: Bolt::PAL::YamlPlan::Parameter

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/pal/yaml_plan/parameter.rb

Constant Summary collapse

PARAMETER_KEYS =
Set['type', 'default', 'description']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(param, definition) ⇒ Parameter

Returns a new instance of Parameter.


11
12
13
14
15
16
17
18
19
# File 'lib/bolt/pal/yaml_plan/parameter.rb', line 11

def initialize(param, definition)
  definition ||= {}
  validate_param(param, definition)

  @name = param
  @value = definition['default']
  @type_expr = Puppet::Pops::Types::TypeParser.singleton.parse(definition['type']) if definition['type']
  @description = definition['description']
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.


7
8
9
# File 'lib/bolt/pal/yaml_plan/parameter.rb', line 7

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.


7
8
9
# File 'lib/bolt/pal/yaml_plan/parameter.rb', line 7

def name
  @name
end

#type_exprObject (readonly)

Returns the value of attribute type_expr.


7
8
9
# File 'lib/bolt/pal/yaml_plan/parameter.rb', line 7

def type_expr
  @type_expr
end

#valueObject (readonly)

Returns the value of attribute value.


7
8
9
# File 'lib/bolt/pal/yaml_plan/parameter.rb', line 7

def value
  @value
end

Instance Method Details

#captures_restObject


35
36
37
# File 'lib/bolt/pal/yaml_plan/parameter.rb', line 35

def captures_rest
  false
end

#transpileObject


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bolt/pal/yaml_plan/parameter.rb', line 39

def transpile
  result = String.new
  result << "\n\s\s"

  # Param type
  if @type_expr.respond_to?(:type_string)
    result << @type_expr.type_string + " "
  elsif !@type_expr.nil?
    result << @type_expr.to_s + " "
  end

  # Param name
  result << "$#{@name}"

  # Param default
  if @value
    default = @type_expr.to_s =~ /String/ ? "'#{@value}'" : @value
    result << " = #{default}"
  end
  result
end

#validate_param(param, definition) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bolt/pal/yaml_plan/parameter.rb', line 21

def validate_param(param, definition)
  unless param.is_a?(String) && param.match?(Bolt::PAL::YamlPlan::VAR_NAME_PATTERN)
    raise Bolt::Error.new("Invalid parameter name #{param.inspect}", "bolt/invalid-plan")
  end

  definition_keys = definition.keys.to_set
  unless PARAMETER_KEYS.superset?(definition_keys)
    invalid_keys = definition_keys - PARAMETER_KEYS
    raise Bolt::Error.new("Plan parameter #{param.inspect} contains illegal key(s)" \
                          " #{invalid_keys.to_a.inspect}",
                          "bolt/invalid-plan")
  end
end