Class: Opulent::Context

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(locals = {}, block = nil, &content) ⇒ Context

Create a context from the environment binding, extended with the locals given as arguments

Parameters:

  • locals (Hash) (defaults to: {})

    Binding extension

  • block (Binding) (defaults to: nil)

    Call environment block

  • content (Binding)

    Content yielding



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

#bindingObject

Returns the value of attribute binding.



10
11
12
# File 'lib/opulent/context.rb', line 10

def binding
  @binding
end

#blockObject

Returns the value of attribute block.



10
11
12
# File 'lib/opulent/context.rb', line 10

def block
  @block
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/opulent/context.rb', line 10

def name
  @name
end

#parentObject

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

Parameters:

  • code (String)

    Code to be evaluated



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_yieldObject

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

Parameters:

  • context (Object)

    Extension 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

Parameters:

  • bind (Binding)

    Binding to extend current context binding



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