Class: Kafo::Param
- Inherits:
-
Object
- Object
- Kafo::Param
- Defined in:
- lib/kafo/param.rb
Constant Summary collapse
- UNSET_VALUES =
['UNSET', :undef]
Instance Attribute Summary collapse
-
#condition ⇒ Object
Returns the value of attribute condition.
-
#doc ⇒ Object
Returns the value of attribute doc.
- #groups ⇒ Object
-
#manifest_default ⇒ Object
Returns the value of attribute manifest_default.
-
#module ⇒ Object
readonly
Returns the value of attribute module.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#value_set ⇒ Object
Returns the value of attribute value_set.
Instance Method Summary collapse
- #<=>(o) ⇒ Object
- #condition_value ⇒ Object
-
#default ⇒ Object
For literal default values, only use ‘manifest_default’.
- #default=(default) ⇒ Object
- #default_to_s ⇒ Object
- #dump_default ⇒ Object
-
#dump_default_needed? ⇒ Boolean
manifest_default may be a variable ($foo::params::bar) and need dumping from Puppet to get the actual default value.
- #identifier ⇒ Object
-
#initialize(builder, name, type) ⇒ Param
constructor
A new instance of Param.
- #manifest_default_params_variable ⇒ Object
- #module_name ⇒ Object
- #multivalued? ⇒ Boolean
- #set_default_from_dump(defaults) ⇒ Object
- #set_value_by_config(config) ⇒ Object
- #to_s ⇒ Object
- #unset_value ⇒ Object
- #valid? ⇒ Boolean
- #validation_errors ⇒ Object
-
#value ⇒ Object
we use @value_set flag because even nil can be valid value Order of descending precedence: @value, @default (from dump), @manifest_default.
- #value=(value) ⇒ Object
- #value_to_s ⇒ Object
- #visible?(context = []) ⇒ Boolean
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
#condition ⇒ Object
Returns the value of attribute condition.
10 11 12 |
# File 'lib/kafo/param.rb', line 10 def condition @condition end |
#doc ⇒ Object
Returns the value of attribute doc.
10 11 12 |
# File 'lib/kafo/param.rb', line 10 def doc @doc end |
#groups ⇒ Object
27 28 29 |
# File 'lib/kafo/param.rb', line 27 def groups @groups || [] end |
#manifest_default ⇒ Object
Returns the value of attribute manifest_default.
9 10 11 |
# File 'lib/kafo/param.rb', line 9 def manifest_default @manifest_default end |
#module ⇒ Object (readonly)
Returns the value of attribute module.
9 10 11 |
# File 'lib/kafo/param.rb', line 9 def module @module end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/kafo/param.rb', line 9 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
9 10 11 |
# File 'lib/kafo/param.rb', line 9 def type @type end |
#value_set ⇒ Object
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_value ⇒ Object
138 139 140 |
# File 'lib/kafo/param.rb', line 138 def condition_value @type.condition_value(value) end |
#default ⇒ Object
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_s ⇒ Object
85 86 87 |
# File 'lib/kafo/param.rb', line 85 def default_to_s internal_value_to_s(default) end |
#dump_default ⇒ Object
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
60 61 62 |
# File 'lib/kafo/param.rb', line 60 def dump_default_needed? manifest_default.to_s.start_with?('$') end |
#identifier ⇒ Object
23 24 25 |
# File 'lib/kafo/param.rb', line 23 def identifier @module ? "#{@module.identifier}::#{name}" : name end |
#manifest_default_params_variable ⇒ Object
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_name ⇒ Object
77 78 79 |
# File 'lib/kafo/param.rb', line 77 def module_name self.module.name end |
#multivalued? ⇒ 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_s ⇒ Object
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_value ⇒ Object
42 43 44 45 |
# File 'lib/kafo/param.rb', line 42 def unset_value @value_set = false @value = nil end |
#valid? ⇒ 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_errors ⇒ Object
118 119 120 |
# File 'lib/kafo/param.rb', line 118 def validation_errors @validation_errors end |
#value ⇒ Object
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_s ⇒ Object
89 90 91 |
# File 'lib/kafo/param.rb', line 89 def value_to_s internal_value_to_s(value) end |
#visible?(context = []) ⇒ Boolean
134 135 136 |
# File 'lib/kafo/param.rb', line 134 def visible?(context = []) condition.nil? || condition.empty? ? true : evaluate_condition(context) end |