Class: RichERB::CustomTag
- Inherits:
-
Object
- Object
- RichERB::CustomTag
- Defined in:
- lib/richerb/custom_tag.rb
Instance Attribute Summary collapse
-
#arity ⇒ Object
Returns the value of attribute arity.
-
#context ⇒ Object
Returns the value of attribute context.
-
#doc ⇒ Object
Returns the value of attribute doc.
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
-
#initialize(options = {}, tag = 'div', root = nil, &block) ⇒ CustomTag
constructor
A new instance of CustomTag.
- #insert(node, &block) ⇒ Object
- #method_missing(method, *args, &block) ⇒ Object
Constructor Details
#initialize(options = {}, tag = 'div', root = nil, &block) ⇒ CustomTag
Returns a new instance of CustomTag.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/richerb/custom_tag.rb', line 8 def initialize = {}, tag = 'div', root = nil, &block if root @doc = root.doc @parent = @doc else @doc = TagNode.new(tag, ) @parent = @doc end @context = nil @arity = nil @ns = nil return unless block_given? @arity = block.arity if @arity <= 0 @context = eval('self', block.binding) instance_eval(&block) else yield self end @parent = @doc end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/richerb/custom_tag.rb', line 35 def method_missing method, *args, &block if @context && @context.respond_to?(method) @context.send(method, *args, &block) else , inner_text = nil, nil if args.size == 1 = args[0] if args[0] == Hash inner_text = args[0] if args[0] != Hash elsif args.size > 1 = args[0] inner_text = args[1] end node = TagNode.new(method.to_s, , inner_text) insert(node, &block) end end |
Instance Attribute Details
#arity ⇒ Object
Returns the value of attribute arity.
6 7 8 |
# File 'lib/richerb/custom_tag.rb', line 6 def arity @arity end |
#context ⇒ Object
Returns the value of attribute context.
5 6 7 |
# File 'lib/richerb/custom_tag.rb', line 5 def context @context end |
#doc ⇒ Object
Returns the value of attribute doc.
3 4 5 |
# File 'lib/richerb/custom_tag.rb', line 3 def doc @doc end |
#parent ⇒ Object
Returns the value of attribute parent.
4 5 6 |
# File 'lib/richerb/custom_tag.rb', line 4 def parent @parent end |
Instance Method Details
#insert(node, &block) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/richerb/custom_tag.rb', line 53 def insert(node, &block) node.parent = @parent if block_given? old_parent = @parent @parent = node @arity ||= block.arity if @arity <= 0 instance_eval(&block) else block.call(@doc) end @parent = old_parent end @parent.children << node end |