Class: Shaven::Scope

Inherits:
Array show all
Defined in:
lib/shaven/scope.rb

Overview

Special kind of array to fetch data from stack of context scopes.

Example

scope = Scope.new({:foo => nil})
scope["foo"] # => nil
scope.unshift({"foo" => "Bar!", "spam" => "Eggs!"})
scope["foo"] # => "Bar!"
scope.unshift({"foo" => "Foobar!"})
scope["foo"] # => "Foobar!"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Scope

Returns a new instance of Scope.



17
18
19
# File 'lib/shaven/scope.rb', line 17

def initialize(*args)
  super(args)
end

Instance Attribute Details

#nodeObject (readonly)

DOM node wrapped by this scope.



15
16
17
# File 'lib/shaven/scope.rb', line 15

def node
  @node
end

Instance Method Details

#[](key) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/shaven/scope.rb', line 21

def [](key)
  each { |scope|
    if scope.key?(key = key.to_s) 
      value = scope[key]
      
      if value.is_a?(Proc) or value.is_a?(Method)
        args = [node, self]
        return value.call(*args.take(value.arity))
      else
        return value
      end
    end
  }
end

#with(node) ⇒ Object

Assigns given DOM node to this context and returns itself. This method will be used to fast switch from one node into another while transformation process.

Example

node_scope = scope.with(node)
node_scope.unshift({"foo" => proc { |node| node.update! :id => "hello-node" }})
node_scope["foo"] # => node


45
46
47
48
# File 'lib/shaven/scope.rb', line 45

def with(node)
  @node = node
  return self
end