Class: Shaven::Scope
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
-
#node ⇒ Object
readonly
DOM node wrapped by this scope.
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#initialize(*args) ⇒ Scope
constructor
A new instance of Scope.
-
#with(node) ⇒ Object
Assigns given DOM node to this context and returns itself.
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
#node ⇒ Object (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 |