Class: RichERB::CustomTag

Inherits:
Object
  • Object
show all
Defined in:
lib/richerb/custom_tag.rb

Direct Known Subclasses

Panel, PanelGrid

Instance Attribute Summary collapse

Instance Method Summary collapse

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 options = {}, tag = 'div', root = nil, &block
if root
  @doc    = root.doc
  @parent = @doc
else
  @doc          = TagNode.new(tag, options)
  @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
    options, inner_text = nil, nil
    if args.size == 1
      options = args[0] if args[0] == Hash
      inner_text = args[0] if args[0] != Hash
    elsif args.size > 1
      options = args[0]
      inner_text = args[1]
    end
    
    node = TagNode.new(method.to_s, options, inner_text)
    insert(node, &block)
  end
end

Instance Attribute Details

#arityObject

Returns the value of attribute arity.



6
7
8
# File 'lib/richerb/custom_tag.rb', line 6

def arity
  @arity
end

#contextObject

Returns the value of attribute context.



5
6
7
# File 'lib/richerb/custom_tag.rb', line 5

def context
  @context
end

#docObject

Returns the value of attribute doc.



3
4
5
# File 'lib/richerb/custom_tag.rb', line 3

def doc
  @doc
end

#parentObject

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