Class: AppArchetype::Template::VariableManager

Inherits:
Object
  • Object
show all
Defined in:
lib/app_archetype/template/variable_manager.rb

Overview

Manages a collection of variables

Instance Method Summary collapse

Constructor Details

#initialize(vars) ⇒ VariableManager

Returns a new instance of VariableManager.



8
9
10
11
12
13
14
15
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

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.

Parameters:

  • method (Symbol)
  • args (Array)

Returns:

  • (Object)


76
77
78
79
80
81
# 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

Instance Method Details

#add(var) ⇒ Object

Adds a variable to the set



42
43
44
# File 'lib/app_archetype/template/variable_manager.rb', line 42

def add(var)
  @data << var
end

#allArray

Returns all variables managed by the variable manager.

Returns:

  • (Array)


22
23
24
# File 'lib/app_archetype/template/variable_manager.rb', line 22

def all
  @data
end

#get(name) ⇒ AppArchetype::Template::Variable

Retrieves a variable by name from the variable manager.

Parameters:

Returns:



33
34
35
# File 'lib/app_archetype/template/variable_manager.rb', line 33

def get(name)
  @data.detect { |var| var.name == name }
end

#to_hHash

Creates a hash representation of variables.

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

Returns:

  • (Hash)


54
55
56
57
58
59
60
61
62
# 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