Class: Inversion::RenderState::Scope

Inherits:
BasicObject
Defined in:
lib/inversion/renderstate.rb

Overview

An encapsulation of the scope in which the bodies of tags evaluate. It’s used to provide a controlled, isolated namespace which remains the same from tag to tag.

Instance Method Summary collapse

Constructor Details

#initialize(locals = {}, fragments = {}) ⇒ Scope

Create a new RenderState::Scope with its initial tag locals set to ‘locals`.



24
25
26
27
# File 'lib/inversion/renderstate.rb', line 24

def initialize( locals={}, fragments={} )
	@locals = locals
	@fragments = fragments
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object (protected)

The main trickery behind this class – intercept tag locals as method calls and map them into values from the Scope’s locals.



68
69
70
71
# File 'lib/inversion/renderstate.rb', line 68

def method_missing( sym, *args, &block )
	return super unless sym =~ /^\w+$/
	return @locals[ sym ].nil? ? @fragments[ sym ] : @locals[ sym ]
end

Instance Method Details

#+(values) ⇒ Object

Return a copy of the receiving Scope merged with the given ‘values`, which can be either another Scope or a Hash.



44
45
46
# File 'lib/inversion/renderstate.rb', line 44

def +( values )
	return Scope.new( self.__locals__.merge(values), self.__fragments__ )
end

#[](name) ⇒ Object

Return the tag local with the specified ‘name`.



31
32
33
# File 'lib/inversion/renderstate.rb', line 31

def []( name )
	return @locals[ name.to_sym ]
end

#[]=(name, value) ⇒ Object

Set the tag local with the specified ‘name` to `value`.



37
38
39
# File 'lib/inversion/renderstate.rb', line 37

def []=( name, value )
	@locals[ name.to_sym ] = value
end

#__fragments__Object

Returns the Hash of rendered fragments that belong to this scope.



57
58
59
# File 'lib/inversion/renderstate.rb', line 57

def __fragments__
	return @fragments
end

#__locals__Object Also known as: to_hash

Return the Hash of tag locals the belongs to this scope.



50
51
52
# File 'lib/inversion/renderstate.rb', line 50

def __locals__
	return @locals
end