Class: Bootstrap::TagMethod

Inherits:
Object
  • Object
show all
Defined in:
mod/bootstrap/lib/bootstrap/component.rb

Instance Method Summary collapse

Constructor Details

#initialize(component, name, html_class, tag_opts = {}, &tag_block) ⇒ TagMethod

Returns a new instance of TagMethod.



4
5
6
7
8
9
10
11
12
13
# File 'mod/bootstrap/lib/bootstrap/component.rb', line 4

def initialize component, name, html_class, tag_opts={}, &tag_block
  @component = component
  @name = name
  @html_class = html_class
  @tag_opts = tag_opts
  @tag_block = tag_block
  @append = []
  @wrap = []
  @xm = Builder::XmlMarkup.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



42
43
44
# File 'mod/bootstrap/lib/bootstrap/component.rb', line 42

def method_missing method, *args, &block
  @component.send method, *args, &block
end

Instance Method Details

#append(&block) ⇒ Object



56
57
58
# File 'mod/bootstrap/lib/bootstrap/component.rb', line 56

def append &block
  @append[-1] << block
end

#call(*args, &content_block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'mod/bootstrap/lib/bootstrap/component.rb', line 15

def call *args, &content_block
  component.content.push "".html_safe

  content, opts = content_block.call
  wrappers = @wrap.pop
  if wrappers.present?
    while wrappers.present? do
      wrapper = wrappers.shift
      if wrapper.is_a? Symbol
        send wrapper, &content_block
      else
        instance_exec(content, &wrappers.shift)
      end
    end
  else
    add_content content
  end

  collected_content = @content.pop
  tag_name = opts.delete(:tag) if tag_name == :yield
  add_content (tag_name, collected_content, opts, false)
  @append.pop.each do |block|
    add_content instance_exec(&block)
  end
  ""
end

#prepend(&block) ⇒ Object



46
47
48
49
50
# File 'mod/bootstrap/lib/bootstrap/component.rb', line 46

def prepend &block
  tmp = @content.pop
  instance_exec &block
  @content << tmp
end

#wrap(&block) ⇒ Object



52
53
54
# File 'mod/bootstrap/lib/bootstrap/component.rb', line 52

def wrap &block
  instance_exec &block
end

#wrapInner(tag = nil, &block) ⇒ Object



60
61
62
# File 'mod/bootstrap/lib/bootstrap/component.rb', line 60

def wrapInner tag=nil, &block
  @wrap[-1] << (block_given? ? block : tag)
end