Class: Radius::Context
- Inherits:
-
Object
- Object
- Radius::Context
- Defined in:
- lib/radius/context.rb
Overview
A context contains the tag definitions which are available for use in a template. See the QUICKSTART for a detailed explaination its usage.
Instance Attribute Summary collapse
-
#definitions ⇒ Object
A hash of tag definition blocks that define tags accessible on a Context.
-
#globals ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#current_nesting ⇒ Object
Returns the state of the current render stack.
-
#define_tag(name, options = {}, &block) ⇒ Object
Creates a tag definition on a context.
-
#initialize(&block) ⇒ Context
constructor
Creates a new Context object.
-
#render_tag(name, attributes = {}, &block) ⇒ Object
Returns the value of a rendered tag.
-
#tag_missing(name, attributes, &block) ⇒ Object
Like method_missing for objects, but fired when a tag is undefined.
-
#with {|_self| ... } ⇒ Object
Yeild an instance of self for tag definitions:.
Constructor Details
#initialize(&block) ⇒ Context
Creates a new Context object.
13 14 15 16 17 18 |
# File 'lib/radius/context.rb', line 13 def initialize(&block) @definitions = {} @tag_binding_stack = [] @globals = DelegatingOpenStruct.new with(&block) if block_given? end |
Instance Attribute Details
#definitions ⇒ Object
A hash of tag definition blocks that define tags accessible on a Context.
9 10 11 |
# File 'lib/radius/context.rb', line 9 def definitions @definitions end |
#globals ⇒ Object
:nodoc:
10 11 12 |
# File 'lib/radius/context.rb', line 10 def globals @globals end |
Instance Method Details
#current_nesting ⇒ Object
Returns the state of the current render stack. Useful from inside a tag definition. Normally just use TagBinding#nesting.
79 80 81 |
# File 'lib/radius/context.rb', line 79 def current_nesting @tag_binding_stack.collect { |tag| tag.name }.join(':') end |
#define_tag(name, options = {}, &block) ⇒ Object
Creates a tag definition on a context. Several options are available to you when creating a tag:
for
-
Specifies an object that the tag is in reference to. This is applicable when a block is not passed to the tag, or when the
expose
option is also used. expose
-
Specifies that child tags should be set for each of the methods contained in this option. May be either a single symbol/string or an array of symbols/strings.
attributes
-
Specifies whether or not attributes should be exposed automatically. Useful for ActiveRecord objects. Boolean. Defaults to
true
.
48 49 50 51 52 |
# File 'lib/radius/context.rb', line 48 def define_tag(name, = {}, &block) type = Util.impartial_hash_delete(, :type).to_s klass = Util.constantize('Radius::TagDefinitions::' + Util.camelize(type) + 'TagFactory') rescue raise(ArgumentError.new("Undefined type `#{type}' in options hash")) klass.new(self).define_tag(name, , &block) end |
#render_tag(name, attributes = {}, &block) ⇒ Object
Returns the value of a rendered tag. Used internally by Parser#parse.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/radius/context.rb', line 55 def render_tag(name, attributes = {}, &block) if name =~ /^(.+?):(.+)$/ render_tag($1) { render_tag($2, attributes, &block) } else tag_definition_block = @definitions[qualified_tag_name(name.to_s)] if tag_definition_block stack(name, attributes, block) do |tag| tag_definition_block.call(tag).to_s end else tag_missing(name, attributes, &block) end end end |
#tag_missing(name, attributes, &block) ⇒ Object
Like method_missing for objects, but fired when a tag is undefined. Override in your own Context to change what happens when a tag is undefined. By default this method raises an UndefinedTagError.
73 74 75 |
# File 'lib/radius/context.rb', line 73 def tag_missing(name, attributes, &block) raise UndefinedTagError.new(name) end |
#with {|_self| ... } ⇒ Object
Yeild an instance of self for tag definitions:
context.with do |c|
c.define_tag 'test' do
'test'
end
end
28 29 30 31 |
# File 'lib/radius/context.rb', line 28 def with yield self self end |