class AppArchetype::Template::Variable

Variable is a class representing a single variable

Constants

BOOLEAN_VALIDATOR

Boolean validation function. Ensures given input is a boolean.

@param [Object] input

@return [Boolean]

DEFAULT_TYPE

Default variable type (String)

DEFAULT_VALIDATOR

Default validation function (string validator)

DEFAULT_VALUES

Default value map

INTEGER_VALIDATOR

Integer validation function. Ensures given input is an integer.

@param [Object] input

@return [Boolean]

MAP_VALIDATOR

Map validation function. Ensures given input is a map

@param [Object] input

@return [Boolean]

STRING_VALIDATOR

String validation function. Ensures given input is indeed a string.

@param [Object] input

@return [Boolean]

VALIDATORS

Maps type to validation function

Attributes

name[R]

Public Class Methods

new(name, spec) click to toggle source
# File lib/app_archetype/template/variable.rb, line 87
def initialize(name, spec)
  @name = name
  @spec = OpenStruct.new(spec)
  @value = @spec.value
end

Public Instance Methods

default() click to toggle source

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.

@return [Object]

# File lib/app_archetype/template/variable.rb, line 117
def default
  return DEFAULT_VALUES[type] unless @spec.default

  @spec.default
end
description() click to toggle source

Returns variable description.

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

@return [String]

# File lib/app_archetype/template/variable.rb, line 131
def description
  return '' unless @spec.description

  @spec.description
end
set!(value) click to toggle source

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

@param [Object] value

# File lib/app_archetype/template/variable.rb, line 102
def set!(value)
  raise 'invalid value' unless valid?(value)

  @value = value
end
type() click to toggle source

Returns variable type.

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

@return [String]

# File lib/app_archetype/template/variable.rb, line 145
def type
  return DEFAULT_TYPE unless @spec.type

  @spec.type
end
valid?(input) click to toggle source

Returns true if the value input is valid.

@param [Object] input

@return [Boolean]

# File lib/app_archetype/template/variable.rb, line 205
def valid?(input)
  validator.call(input)
end
validator() click to toggle source

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

@return [Proc]

# File lib/app_archetype/template/variable.rb, line 191
def validator
  validator = VALIDATORS[@spec.type]
  validator ||= DEFAULT_VALIDATOR

  validator
end
value() click to toggle source

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.

@return [String]

# File lib/app_archetype/template/variable.rb, line 167
def value
  return default if @value.nil?
  return call_helper if method?

  @value
end
value?() click to toggle source

Returns true if value has been set

@return [Boolean]

# File lib/app_archetype/template/variable.rb, line 178
def value?
  !@value.nil?
end

Private Instance Methods

call_helper() click to toggle source

Calls a helper function to generate a value

# File lib/app_archetype/template/variable.rb, line 229
def call_helper
  method = deserialize_method(@value)
  helpers.send(method.name, *method.args)
end
deserialize_method(method) click to toggle source

Creates a struct representing a method call to be made to resolve a variable value

# File lib/app_archetype/template/variable.rb, line 236
def deserialize_method(method)
  method = method.delete('#')
  parts = method.split(',')
  name = parts.shift

  args = parts || []

  OpenStruct.new(
    name: name.to_sym,
    args: args
  )
end
helpers() click to toggle source

Returns an object which extends helpers module.

This is used for calling helper functions

# File lib/app_archetype/template/variable.rb, line 214
def helpers
  Object
    .new
    .extend(AppArchetype::Template::Helpers)
end
method?() click to toggle source

Returns true if variable value begins with `#` this indicates the variable value has been set to a function

# File lib/app_archetype/template/variable.rb, line 222
def method?
  return false unless @value.is_a?(String)

  @value[0, 1] == '#'
end