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.



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

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.



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

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.



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

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

#[](name) ⇒ Object

Return the tag local with the specified name.



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

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

#[]=(name, value) ⇒ Object

Set the tag local with the specified name to value.



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

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

#__fragments__Object

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



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

def __fragments__
	return @fragments
end

#__locals__Object Also known as: to_hash

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



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

def __locals__
	return @locals
end