Class: Tablette::Element
- Inherits:
-
Object
- Object
- Tablette::Element
- Includes:
- ActiveSupport::Configurable
- Defined in:
- lib/tablette/element.rb,
lib/tablette/element/nesting.rb,
lib/tablette/element/rendering.rb,
lib/tablette/element/configuration.rb
Instance Attribute Summary collapse
-
#helper ⇒ Object
Returns the value of attribute helper.
-
#renderer ⇒ Object
Returns the value of attribute renderer.
Instance Method Summary collapse
- #element_to_html(element, *args) ⇒ Object
-
#initialize(*args, &definition) ⇒ Element
constructor
A new instance of Element.
- #method_missing(method, *args, &block) ⇒ Object
- #respond_to_missing?(method, include_private = false) ⇒ Boolean
-
#to_html(*args) ⇒ Object
Translates element to html tag.
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) = args. if .include? :renderer @renderer = .delete :renderer end if .include? :helper @helper = .delete :helper end instance_exec(&definition) if block_given? config.merge!() 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..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
#helper ⇒ Object
Returns the value of attribute helper.
5 6 7 |
# File 'lib/tablette/element.rb', line 5 def helper @helper end |
#renderer ⇒ Object
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
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..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, , , = ([:tag, :override_html_options, :html_options, :builtin_html_options], *args) raise "Set tag option for #{self.class.name}" if tag.blank? = .merge .each do |key, value| if .include? key [key] = "#{value} #{[key]}" else [key] = value end end @renderer.element = self begin @renderer.produce_element tag, , html_content(*args) ensure @renderer.element = nil end end |