Class: Bootstrap::OldComponent

Inherits:
Component show all
Includes:
Delegate
Defined in:
mod/bootstrap/lib/bootstrap/old_component.rb

Direct Known Subclasses

Component::Layout, Component::Panel

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Delegate

#method_missing, #respond_to_missing?

Methods inherited from Component

def_simple_tag_method

Constructor Details

#initialize(context, *args, &block) ⇒ OldComponent

Returns a new instance of OldComponent.



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

def initialize context, *args, &block
  @context = context
  @content = ["".html_safe]
  @args = args
  @child_args = []
  @append = []
  @wrap = []
  @build_block = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Bootstrap::Delegate

Class Method Details

.add_div_method(name, html_class, opts = {}, &tag_block) ⇒ Object Also known as: def_div_method

Like add_tag_method but always generates a div tag The tag option is not available



21
22
23
# File 'mod/bootstrap/lib/bootstrap/old_component.rb', line 21

def add_div_method name, html_class, opts={}, &tag_block
  add_tag_method name, html_class, opts.merge(tag: :div), &tag_block
end

.add_tag_method(name, html_class, tag_opts = {}, &tag_block) ⇒ Object Also known as: def_tag_method

Defines a method that generates a html tag

Examples:

add_tag_method :link, "known-link", tag: :a, id: "uniq-link"
link  # => <a class="known-link" id="uniq-link"></a>

Parameters:

  • name (Symbol, String)

    the name of the method. If no :tag option in tag_opts is defined then the name is also the name of the tag that the method generates

  • html_class (String)

    a html class that is added to tag. Use nil if you don't want a html_class

  • tag_opts (Hash) (defaults to: {})

    additional argument that will be added to the tag

Options Hash (tag_opts):

  • tag (Symbol, String)

    the name of the tag



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'mod/bootstrap/lib/bootstrap/old_component.rb', line 33

def add_tag_method name, html_class, tag_opts={}, &tag_block
  define_method name do |*args, &block|
    process_tag tag_opts[:tag] || name do
      content, opts, new_child_args = standardize_args args, &tag_block
      add_classes opts, html_class, tag_opts.delete(:optional_classes)
      if (attributes = tag_opts.delete(:attributes))
        opts.merge! attributes
      end

      content = with_child_args new_child_args do
        generate_content content,
                         tag_opts[:content_processor],
                         &block
      end

      [content, opts]
    end
  end
end

.render(format, *args, &block) ⇒ Object



15
16
17
# File 'mod/bootstrap/lib/bootstrap/old_component.rb', line 15

def render format, *args, &block
  new(format, *args, &block).render
end

Instance Method Details

#append(&block) ⇒ Object



74
75
76
# File 'mod/bootstrap/lib/bootstrap/old_component.rb', line 74

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

#insert(&block) ⇒ Object



70
71
72
# File 'mod/bootstrap/lib/bootstrap/old_component.rb', line 70

def insert &block
  instance_exec &block
end

#prepend(&block) ⇒ Object



64
65
66
67
68
# File 'mod/bootstrap/lib/bootstrap/old_component.rb', line 64

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

#renderObject



57
58
59
60
61
62
# File 'mod/bootstrap/lib/bootstrap/old_component.rb', line 57

def render
  @rendered = begin
    render_content
    @content[-1]
  end
end

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



78
79
80
# File 'mod/bootstrap/lib/bootstrap/old_component.rb', line 78

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