Class: AppArchetype::Template::Variable

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

Returns:

  • (Boolean)
lambda do |input|
  input.is_a?(String)
end
BOOLEAN_VALIDATOR =

Boolean validation function. Ensures given input is a boolean.

Returns:

  • (Boolean)
lambda do |input|
  [true, false, 'true', 'false'].include?(input)
end
INTEGER_VALIDATOR =

Integer validation function. Ensures given input is an integer.

Returns:

  • (Boolean)
lambda do |input|
  input != '0' && input.to_i != 0
end
MAP_VALIDATOR =

Map validation function. Ensures given input is a map

Returns:

  • (Boolean)
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

Instance Method Summary collapse

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

#nameObject (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

#defaultObject

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.

Returns:

  • (Object)


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

#descriptionString

Returns variable description.

In the event the manifest does not specify a description an empty string will be returned.

Returns:



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

Parameters:

  • value (Object)


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

#typeString

Returns variable type.

In the event the manifest does not specify a type, the default type of String will be returned.

Returns:



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.

Parameters:

  • input (Object)

Returns:

  • (Boolean)


205
206
207
# File 'lib/app_archetype/template/variable.rb', line 205

def valid?(input)
  validator.call(input)
end

#validatorProc

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

Returns:

  • (Proc)


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

#valueString

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.

Returns:



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

Returns:

  • (Boolean)


178
179
180
# File 'lib/app_archetype/template/variable.rb', line 178

def value?
  !@value.nil?
end