Class: Tablette::Element

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Configurable
Defined in:
lib/tablette/element.rb,
lib/tablette/element/nesting.rb,
lib/tablette/element/rendering.rb,
lib/tablette/element/configuration.rb

Direct Known Subclasses

Column, Row, Section, Table

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &definition) ⇒ Element

Returns a new instance of Element.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/tablette/element.rb', line 7

def initialize(*args, &definition)
  options = args.extract_options!
  if options.include? :renderer
    @renderer = options.delete :renderer
  end
  
  if options.include? :helper
    @helper = options.delete :helper
  end
  
  instance_exec(&definition) if block_given?
  config.merge!(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/tablette/element.rb', line 25

def method_missing(method, *args, &block)
  if @helper.respond_to? method
    @helper.send method, *args, &block
  elsif config.allowed_configuration_options.include?(method.to_s)
    # Catches a call to configuration option setter.
    #
    # Example:
    # body do
    #   html_options { class: 'test' } # Holds such calls
    # end
  
    config[method] = args.first || block
  
    class_eval do
      define_method method do |value|
        config[method] = value
      end
      protected method
    end
  
    config[method]
  else
    super
  end
end

Instance Attribute Details

#helperObject

Returns the value of attribute helper.



5
6
7
# File 'lib/tablette/element.rb', line 5

def helper
  @helper
end

#rendererObject

Returns the value of attribute renderer.



5
6
7
# File 'lib/tablette/element.rb', line 5

def renderer
  @renderer
end

Instance Method Details

#element_to_html(element, *args) ⇒ Object



28
29
30
31
32
# File 'lib/tablette/element/rendering.rb', line 28

def element_to_html(element, *args)
  send(element).map do |item|
    item.to_html(*args)
  end
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/tablette/element.rb', line 21

def respond_to_missing?(method, include_private = false)
  super || @helper.respond_to?(method, include_private) || config.allowed_configuration_options.include?(method.to_s)
end

#to_html(*args) ⇒ Object

Translates element to html tag.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tablette/element/rendering.rb', line 4

def to_html(*args)
  tag, override_html_options, user_html_options, builtin_html_options =
    get_options([:tag, :override_html_options, :html_options, :builtin_html_options], *args)

  raise "Set tag option for #{self.class.name}" if tag.blank?

  html_options = override_html_options.merge user_html_options

  builtin_html_options.each do |key, value|
    if html_options.include? key
      html_options[key] = "#{value} #{html_options[key]}"
    else
      html_options[key] = value
    end
  end
  
  @renderer.element = self
  begin
    @renderer.produce_element tag, html_options, html_content(*args)
  ensure
    @renderer.element = nil
  end
end