class AppArchetype::Template::VariableManager

Manages a collection of variables

Public Class Methods

new(vars) click to toggle source
# File lib/app_archetype/template/variable_manager.rb, line 8
def initialize(vars)
  vars ||= []
  @data = []

  vars.each do |name, spec|
    @data << AppArchetype::Template::Variable.new(name, spec)
  end
end

Public Instance Methods

add(var) click to toggle source

Adds a variable to the set

@param [AppArchetype::Template::Variable] var

# File lib/app_archetype/template/variable_manager.rb, line 42
def add(var)
  @data << var
end
all() click to toggle source

Returns all variables managed by the variable manager.

@return [Array]

# File lib/app_archetype/template/variable_manager.rb, line 22
def all
  @data
end
get(name) click to toggle source

Retrieves a variable by name from the variable manager.

@param [String] name

@return [AppArchetype::Template::Variable]

# File lib/app_archetype/template/variable_manager.rb, line 33
def get(name)
  @data.detect { |var| var.name == name }
end
method_missing(method, *args) click to toggle source

Method missing retrieves variable from manager and returns the value to the caller if it is found.

When a call is made to an undefined variable, a MethodMissing error will be raised.

@param [Symbol] method @param [Array] args

@return [Object]

Calls superclass method
# File lib/app_archetype/template/variable_manager.rb, line 76
def method_missing(method, *args)
  var = get(method.to_s)
  return var.value if var

  super
end
to_h() click to toggle source

Creates a hash representation of variables.

The variable name is the key, and the currrent value is the value.

@return [Hash]

# File lib/app_archetype/template/variable_manager.rb, line 54
def to_h
  var_hash = {}

  @data.each do |var|
    var_hash[var.name] = var.value
  end

  var_hash
end