Class: Bricolage::Variables
- Inherits:
-
Object
- Object
- Bricolage::Variables
- Defined in:
- lib/bricolage/variables.rb
Class Method Summary collapse
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
- #add(var) ⇒ Object
- #each_variable(&block) ⇒ Object
- #get_force(name) ⇒ Object
-
#initialize ⇒ Variables
constructor
A new instance of Variables.
- #inspect ⇒ Object
- #keys ⇒ Object
-
#resolve ⇒ Object
resolve self recursively.
- #resolve_with(resolved) ⇒ Object
- #resolved? ⇒ Boolean
- #update(vars) ⇒ Object
Constructor Details
#initialize ⇒ Variables
Returns a new instance of Variables.
20 21 22 |
# File 'lib/bricolage/variables.rb', line 20 def initialize @vars = {} end |
Class Method Details
.define ⇒ Object
14 15 16 17 18 |
# File 'lib/bricolage/variables.rb', line 14 def Variables.define new.tap {|vars| yield vars } end |
.union(*vars_list) ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/bricolage/variables.rb', line 6 def Variables.union(*vars_list) new.tap {|result| vars_list.each do |vars| result.update vars end } end |
Instance Method Details
#[](name) ⇒ Object
36 37 38 39 |
# File 'lib/bricolage/variables.rb', line 36 def [](name) var = @vars[name] or raise ParameterError, "no such variable: #{name}" var.value end |
#[]=(name, value) ⇒ Object
46 47 48 |
# File 'lib/bricolage/variables.rb', line 46 def []=(name, value) add Variable.new(name, value) end |
#add(var) ⇒ Object
54 55 56 |
# File 'lib/bricolage/variables.rb', line 54 def add(var) @vars[var.name] = var end |
#each_variable(&block) ⇒ Object
32 33 34 |
# File 'lib/bricolage/variables.rb', line 32 def each_variable(&block) @vars.each_value(&block) end |
#get_force(name) ⇒ Object
41 42 43 44 |
# File 'lib/bricolage/variables.rb', line 41 def get_force(name) var = @vars[name] var ? var.value : nil end |
#inspect ⇒ Object
24 25 26 |
# File 'lib/bricolage/variables.rb', line 24 def inspect "\#<#{self.class} #{@vars.inspect}>" end |
#keys ⇒ Object
50 51 52 |
# File 'lib/bricolage/variables.rb', line 50 def keys @vars.keys end |
#resolve ⇒ Object
resolve self recursively
65 66 67 68 69 70 71 |
# File 'lib/bricolage/variables.rb', line 65 def resolve ResolvedVariables.define {|resolved| @vars.each_value do |var| resolved.add (var, resolved, {}) end } end |
#resolve_with(resolved) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/bricolage/variables.rb', line 98 def resolve_with(resolved) raise "[BUG] unresolved variables given" unless resolved.resolved? ResolvedVariables.define {|result| each_variable do |var| if var.resolved? result.add var else val = resolved.(var.value.to_s) result.add ResolvedVariable.new(var.name, val) end end } end |
#resolved? ⇒ Boolean
28 29 30 |
# File 'lib/bricolage/variables.rb', line 28 def resolved? false end |
#update(vars) ⇒ Object
58 59 60 61 62 |
# File 'lib/bricolage/variables.rb', line 58 def update(vars) vars.each_variable do |var| add var end end |