Class: Kafo::Param

Inherits:
Object
  • Object
show all
Defined in:
lib/kafo/param.rb

Constant Summary collapse

UNSET_VALUES =
['UNSET', :undef]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(builder, name, type) ⇒ Param

Returns a new instance of Param.



13
14
15
16
17
18
19
20
21
# File 'lib/kafo/param.rb', line 13

def initialize(builder, name, type)
  @name   = name
  @module = builder
  @type   = DataType.new_from_string(type)
  @default = nil
  @value_set = false
  @groups = nil
  @validation_errors = []
end

Instance Attribute Details

#conditionObject

Returns the value of attribute condition.



10
11
12
# File 'lib/kafo/param.rb', line 10

def condition
  @condition
end

#docObject

Returns the value of attribute doc.



10
11
12
# File 'lib/kafo/param.rb', line 10

def doc
  @doc
end

#groupsObject



27
28
29
# File 'lib/kafo/param.rb', line 27

def groups
  @groups || []
end

#manifest_defaultObject

Returns the value of attribute manifest_default.



9
10
11
# File 'lib/kafo/param.rb', line 9

def manifest_default
  @manifest_default
end

#moduleObject (readonly)

Returns the value of attribute module.



9
10
11
# File 'lib/kafo/param.rb', line 9

def module
  @module
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/kafo/param.rb', line 9

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/kafo/param.rb', line 9

def type
  @type
end

#value_setObject

Returns the value of attribute value_set.



10
11
12
# File 'lib/kafo/param.rb', line 10

def value_set
  @value_set
end

Instance Method Details

#<=>(o) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/kafo/param.rb', line 126

def <=> o
  unless @module.configuration.app[:no_prefix]
    r = self.module_name <=> o.module_name
    return r unless r == 0
  end
  self.name <=> o.name
end

#condition_valueObject



138
139
140
# File 'lib/kafo/param.rb', line 138

def condition_value
  @type.condition_value(value)
end

#defaultObject

For literal default values, only use ‘manifest_default’. For variable or values from a data lookup, use the value loaded back from the dump in ‘default’.



49
50
51
# File 'lib/kafo/param.rb', line 49

def default
  @type.typecast(dump_default_needed? || !@default.nil? ? @default : manifest_default)
end

#default=(default) ⇒ Object



53
54
55
56
# File 'lib/kafo/param.rb', line 53

def default=(default)
  default = nil if UNSET_VALUES.include?(default)
  @default = default
end

#default_to_sObject



85
86
87
# File 'lib/kafo/param.rb', line 85

def default_to_s
  internal_value_to_s(default)
end

#dump_defaultObject



64
65
66
# File 'lib/kafo/param.rb', line 64

def dump_default
  @type.dump_default(manifest_default_params_variable)
end

#dump_default_needed?Boolean

manifest_default may be a variable ($foo::params::bar) and need dumping from Puppet to get the actual default value

Returns:

  • (Boolean)


60
61
62
# File 'lib/kafo/param.rb', line 60

def dump_default_needed?
  manifest_default.to_s.start_with?('$')
end

#identifierObject



23
24
25
# File 'lib/kafo/param.rb', line 23

def identifier
  @module ? "#{@module.identifier}::#{name}" : name
end

#manifest_default_params_variableObject



73
74
75
# File 'lib/kafo/param.rb', line 73

def manifest_default_params_variable
  manifest_default[1..-1] if dump_default_needed?
end

#module_nameObject



77
78
79
# File 'lib/kafo/param.rb', line 77

def module_name
  self.module.name
end

#multivalued?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/kafo/param.rb', line 122

def multivalued?
  @type.multivalued?
end

#set_default_from_dump(defaults) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/kafo/param.rb', line 93

def set_default_from_dump(defaults)
  # if we don't have default value from dump (can happen for modules added from hooks,
  # or without using a params class), the existing default value from the manifest will
  # be used. On calling #value, the default will be returned if no overriding value is set.
  if dump_default_needed? && defaults.has_key?(manifest_default_params_variable)
    self.default = defaults[manifest_default_params_variable]
  elsif defaults.has_key?(identifier)
    self.default = defaults[identifier]
  end
end

#set_value_by_config(config) ⇒ Object



104
105
106
107
# File 'lib/kafo/param.rb', line 104

def set_value_by_config(config)
  base       = config[self.module.class_name]
  self.value = base[name] if base.has_key?(name)
end

#to_sObject



81
82
83
# File 'lib/kafo/param.rb', line 81

def to_s
  "#<#{self.class}:#{self.object_id} @name=#{name.inspect} @default=#{default.inspect} @value=#{value.inspect} @type=#{@type}>"
end

#unset_valueObject



42
43
44
45
# File 'lib/kafo/param.rb', line 42

def unset_value
  @value_set = false
  @value     = nil
end

#valid?Boolean

Returns:

  • (Boolean)


109
110
111
112
113
114
115
116
# File 'lib/kafo/param.rb', line 109

def valid?
  @validation_errors = []

  # run data type based validations, append errors
  @type.valid?(value, @validation_errors)

  @validation_errors.empty?
end

#validation_errorsObject



118
119
120
# File 'lib/kafo/param.rb', line 118

def validation_errors
  @validation_errors
end

#valueObject

we use @value_set flag because even nil can be valid value Order of descending precedence: @value, @default (from dump), @manifest_default



33
34
35
# File 'lib/kafo/param.rb', line 33

def value
  @value_set ? @type.typecast(@value) : default
end

#value=(value) ⇒ Object



37
38
39
40
# File 'lib/kafo/param.rb', line 37

def value=(value)
  @value_set = true
  @value     = normalize_value(value)
end

#value_to_sObject



89
90
91
# File 'lib/kafo/param.rb', line 89

def value_to_s
  internal_value_to_s(value)
end

#visible?(context = []) ⇒ Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/kafo/param.rb', line 134

def visible?(context = [])
  condition.nil? || condition.empty? ? true : evaluate_condition(context)
end