Class: AppArchetype::Template::Variable
- Inherits:
-
Object
- Object
- AppArchetype::Template::Variable
- Defined in:
- lib/app_archetype/template/variable.rb
Overview
Variable is a class representing a single variable
Constant Summary collapse
- DEFAULT_TYPE =
Default variable type (String)
'string'.freeze
- DEFAULT_VALUES =
Default value map
{ 'string' => '', 'boolean' => false, 'integer' => 0, 'map' => {} }.freeze
- STRING_VALIDATOR =
String validation function. Ensures given input is indeed a string.
lambda do |input| input.is_a?(String) end
- BOOLEAN_VALIDATOR =
Boolean validation function. Ensures given input is a boolean.
lambda do |input| [true, false, 'true', 'false'].include?(input) end
- INTEGER_VALIDATOR =
Integer validation function. Ensures given input is an integer.
lambda do |input| input != '0' && input.to_i != 0 end
- MAP_VALIDATOR =
Map validation function. Ensures given input is a map
lambda do |input| input.is_a?(Hash) end
- VALIDATORS =
Maps type to validation function
{ 'string' => STRING_VALIDATOR, 'boolean' => BOOLEAN_VALIDATOR, 'integer' => INTEGER_VALIDATOR, 'map' => MAP_VALIDATOR }.freeze
- DEFAULT_VALIDATOR =
Default validation function (string validator)
STRING_VALIDATOR
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#default ⇒ Object
Returns default value for the variable.
-
#description ⇒ String
Returns variable description.
-
#initialize(name, spec) ⇒ Variable
constructor
A new instance of Variable.
-
#set!(value) ⇒ Object
Sets value of variable so long as it’s valid.
-
#type ⇒ String
Returns variable type.
-
#valid?(input) ⇒ Boolean
Returns true if the value input is valid.
-
#validator ⇒ Proc
Retrieves the appropriate validator function basedd on the specified type.
-
#value ⇒ String
Returns variable value.
-
#value? ⇒ Boolean
Returns true if value has been set.
Constructor Details
#initialize(name, spec) ⇒ Variable
Returns a new instance of Variable.
87 88 89 90 91 |
# File 'lib/app_archetype/template/variable.rb', line 87 def initialize(name, spec) @name = name @spec = OpenStruct.new(spec) @value = @spec.value end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
85 86 87 |
# File 'lib/app_archetype/template/variable.rb', line 85 def name @name end |
Instance Method Details
#default ⇒ Object
Returns default value for the variable.
In the event the manifest does not specify a default one will be picked from the DEFAULT_VALUES map based on the variable’s type.
117 118 119 120 121 |
# File 'lib/app_archetype/template/variable.rb', line 117 def default return DEFAULT_VALUES[type] unless @spec.default @spec.default end |
#description ⇒ String
Returns variable description.
In the event the manifest does not specify a description an empty string will be returned.
131 132 133 134 135 |
# File 'lib/app_archetype/template/variable.rb', line 131 def description return '' unless @spec.description @spec.description end |
#set!(value) ⇒ Object
Sets value of variable so long as it’s valid.
A runtime error will be raised if the valdiation fails for the given value.
Has a side effect of setting @value instance variable
102 103 104 105 106 |
# File 'lib/app_archetype/template/variable.rb', line 102 def set!(value) raise 'invalid value' unless valid?(value) @value = value end |
#type ⇒ String
Returns variable type.
In the event the manifest does not specify a type, the default type of String will be returned.
145 146 147 148 149 |
# File 'lib/app_archetype/template/variable.rb', line 145 def type return DEFAULT_TYPE unless @spec.type @spec.type end |
#valid?(input) ⇒ Boolean
Returns true if the value input is valid.
205 206 207 |
# File 'lib/app_archetype/template/variable.rb', line 205 def valid?(input) validator.call(input) end |
#validator ⇒ Proc
Retrieves the appropriate validator function basedd on the specified type.
If a type is not set then a string validator function is returned by default
191 192 193 194 195 196 |
# File 'lib/app_archetype/template/variable.rb', line 191 def validator validator = VALIDATORS[@spec.type] validator ||= DEFAULT_VALIDATOR validator end |
#value ⇒ String
Returns variable value.
If the value has not been set (i.e. overridden) then the default value will be returned.
Values set beginning with ‘#` are passed into the helpers class and evaluated as functions. That permits the manifest to use string helpers as values from the manifest.
Function calls must be in the format ‘#method_name,arg1,arg2` for example to call the join function `#join,.,biggerconcept,com` will result in `biggerconcept.com` becoming the value.
167 168 169 170 171 172 |
# File 'lib/app_archetype/template/variable.rb', line 167 def value return default if @value.nil? return call_helper if method? @value end |
#value? ⇒ Boolean
Returns true if value has been set
178 179 180 |
# File 'lib/app_archetype/template/variable.rb', line 178 def value? !@value.nil? end |