Class: Fluid::GlobalVar

Inherits:
Var
  • Object
show all
Defined in:
lib/fluid.rb

Overview

GlobalVars are the subclass that handles binding and unbinding of Ruby globals.

Instance Attribute Summary

Attributes inherited from Var

#name, #original_name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Var

global?, #initialize

Constructor Details

This class inherits a constructor from Fluid::Var

Class Method Details

.build(original_name, environment, value_destructor) ⇒ Object

:nodoc:



138
139
140
# File 'lib/fluid.rb', line 138

def GlobalVar.build(original_name, environment, value_destructor)
  new(original_name, environment, value_destructor)
end

Instance Method Details

#assert_acceptable_nameObject



170
171
172
173
174
175
# File 'lib/fluid.rb', line 170

def assert_acceptable_name
  if [:$!, :$stdin, :$stdout, :$stderr].include?(name)
    raise NameError, 
      "'#{name}' is not allowed in Fluid.let. It's too iffy to get it right."
  end
end

#createObject

The original value of the global is, in effect, an outermost binding, one not created with Fluid.let. A binding has to be made here so that unbinding works on exit from the Fluid.let block.



145
146
147
148
# File 'lib/fluid.rb', line 145

def create
  super
  push_binding(instance_eval("#{name.to_s}"))
end

#pop_bindingObject



159
160
161
162
# File 'lib/fluid.rb', line 159

def pop_binding
  super
  set_global
end

#push_binding(value) ⇒ Object

The environment really just holds values for unbinding. Access to the newly-bound variable within the Fluid.let block is through the global itself. So it needs to be set and reset by these methods.



154
155
156
157
# File 'lib/fluid.rb', line 154

def push_binding(value)
  super
  set_global
end

#set_globalObject



164
165
166
167
168
# File 'lib/fluid.rb', line 164

def set_global
  evalme = "#{name.to_s} = @environment.get(#{name.inspect})"
  # puts evalme
  instance_eval evalme
end