Class: DynamicVariable::Mixin::MixedDynamicVariable

Inherits:
DynamicVariable show all
Defined in:
lib/dynamic_variable.rb

Instance Attribute Summary

Attributes inherited from DynamicVariable

#default_variable

Instance Method Summary collapse

Methods inherited from DynamicVariable

#bindings=, #method_missing, #variables=, #with

Constructor Details

#initialize(mix) ⇒ MixedDynamicVariable

Returns a new instance of MixedDynamicVariable.



136
137
138
139
# File 'lib/dynamic_variable.rb', line 136

def initialize(mix)
  super()
  @mix = mix
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class DynamicVariable

Instance Method Details

#[](variable) ⇒ Object



174
175
176
# File 'lib/dynamic_variable.rb', line 174

def [](variable)
  find_binding(variable)[1] = @mix.send(variable)
end

#[]=(variable, value) ⇒ Object



178
179
180
# File 'lib/dynamic_variable.rb', line 178

def []=(variable, value)
  @mix.send(variable.to_s+"=", super)
end

#bindings(*args) ⇒ Object



192
193
194
195
# File 'lib/dynamic_variable.rb', line 192

def bindings(*args)
  variables
  super
end

#pop(variable) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/dynamic_variable.rb', line 159

def pop(variable)
  value = super
  begin
    old_value = find_binding(variable)[1]
  rescue ArgumentError
    return value
  end
  @mix.send(variable.to_s+"=", old_value)
  value
ensure
  if @bindings.count{|var, value| var == variable} == 1
    super
  end
end

#push(variable, value) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/dynamic_variable.rb', line 141

def push(variable, value)
  old_value = @mix.send(variable)
  begin
    pair = begin
      find_binding(variable)
    rescue ArgumentError
      super
    end
    pair[1] = old_value
  
    @mix.send(variable.to_s+"=", value)
    super
  rescue Exception
    @bindings.pop
    raise
  end
end

#set_bindings(*args) ⇒ Object



197
198
199
200
201
# File 'lib/dynamic_variable.rb', line 197

def set_bindings(*args)
  bindings = super
  self.variables = original_variables
  bindings
end

#variablesObject



185
186
187
188
189
190
# File 'lib/dynamic_variable.rb', line 185

def variables
  variables = super
  variables.each_key do |variable|
    variables[variable] = self[variable]
  end
end