Class: Opulent::Context
- Inherits:
-
Object
- Object
- Opulent::Context
- Defined in:
- lib/opulent/context.rb
Overview
The context class is used to differentiate local, instance and class variables and to define the current working environment. Each class, method and instance has its own context
Instance Attribute Summary collapse
-
#binding ⇒ Object
Returns the value of attribute binding.
-
#block ⇒ Object
Returns the value of attribute block.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
-
#evaluate(code, &block) ⇒ Object
Evaluate ruby code in current context.
-
#evaluate_yield ⇒ Object
Call given input block and return the output.
-
#extend_locals(locals) ⇒ Object
Extend the call context with a Hash, String or other Object.
-
#extend_nonlocals(bind) ⇒ Object
Extend instance, class and global variables for use in definitions.
-
#initialize(locals = {}, block = nil, &content) ⇒ Context
constructor
Create a context from the environment binding, extended with the locals given as arguments.
Constructor Details
#initialize(locals = {}, block = nil, &content) ⇒ Context
Create a context from the environment binding, extended with the locals given as arguments
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/opulent/context.rb', line 19 def initialize(locals = {}, block = nil, &content) @content = content @block = block if @block @binding = @block.binding.clone else @binding = Binding.new.get end extend_locals locals end |
Instance Attribute Details
#binding ⇒ Object
Returns the value of attribute binding.
10 11 12 |
# File 'lib/opulent/context.rb', line 10 def binding @binding end |
#block ⇒ Object
Returns the value of attribute block.
10 11 12 |
# File 'lib/opulent/context.rb', line 10 def block @block end |
#name ⇒ Object
Returns the value of attribute name.
10 11 12 |
# File 'lib/opulent/context.rb', line 10 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
10 11 12 |
# File 'lib/opulent/context.rb', line 10 def parent @parent end |
Instance Method Details
#evaluate(code, &block) ⇒ Object
Evaluate ruby code in current context
36 37 38 39 40 41 42 |
# File 'lib/opulent/context.rb', line 36 def evaluate(code, &block) begin eval code, @binding, &block rescue NameError => variable Compiler.error :binding, variable, code end end |
#evaluate_yield ⇒ Object
Call given input block and return the output
46 47 48 |
# File 'lib/opulent/context.rb', line 46 def evaluate_yield @content.call if @content end |
#extend_locals(locals) ⇒ Object
Extend the call context with a Hash, String or other Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/opulent/context.rb', line 54 def extend_locals(locals) # Create new local variables from the input hash locals.each do |key, value| begin @binding.local_variable_set key.to_sym, value rescue NameError => variable Compiler.error :variable_name, variable, key end end end |
#extend_nonlocals(bind) ⇒ Object
Extend instance, class and global variables for use in definitions
69 70 71 72 73 74 75 76 77 |
# File 'lib/opulent/context.rb', line 69 def extend_nonlocals(bind) bind.eval('instance_variables').each do |var| @binding.eval('self').instance_variable_set var, bind.eval(var.to_s) end bind.eval('self.class.class_variables').each do |var| @binding.eval('self').class_variable_set var, bind.eval(var.to_s) end end |