Class: AppArchetype::Template::VariableManager
- Inherits:
-
Object
- Object
- AppArchetype::Template::VariableManager
- Defined in:
- lib/app_archetype/template/variable_manager.rb
Overview
Manages a collection of variables
Instance Method Summary collapse
-
#add(var) ⇒ Object
Adds a variable to the set.
-
#all ⇒ Array
Returns all variables managed by the variable manager.
-
#get(name) ⇒ AppArchetype::Template::Variable
Retrieves a variable by name from the variable manager.
-
#initialize(vars) ⇒ VariableManager
constructor
A new instance of VariableManager.
-
#method_missing(method, *args) ⇒ Object
Method missing retrieves variable from manager and returns the value to the caller if it is found.
-
#to_h ⇒ Hash
Creates a hash representation of variables.
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.
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 |
#all ⇒ Array
Returns all variables managed by the variable manager.
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.
33 34 35 |
# File 'lib/app_archetype/template/variable_manager.rb', line 33 def get(name) @data.detect { |var| var.name == name } end |
#to_h ⇒ Hash
Creates a hash representation of variables.
The variable name is the key, and the currrent value is the value.
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 |