Class: Theme::Component
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Theme::Component
- Includes:
- Events
- Defined in:
- lib/theme/component.rb
Class Attribute Summary collapse
-
.html ⇒ Object
readonly
Returns the value of attribute html.
-
.id(name) ⇒ Object
readonly
Returns the value of attribute id.
-
.node ⇒ Object
Returns the value of attribute node.
-
.path ⇒ Object
readonly
Returns the value of attribute path.
Instance Attribute Summary collapse
-
#instance ⇒ Object
readonly
Returns the value of attribute instance.
-
#node ⇒ Object
Returns the value of attribute node.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(instance = false) ⇒ Component
constructor
A new instance of Component.
- #method_missing(method, *args, &block) ⇒ Object
- #render(meth = 'display', options = {}) ⇒ Object
- #set_locals(options) ⇒ Object
Methods included from Events
#add_listener, included, #notify_listeners, #trigger
Constructor Details
#initialize(instance = false) ⇒ Component
Returns a new instance of Component.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/theme/component.rb', line 12 def initialize instance = false @instance = instance @node = self.class.node.clone if self.class.node @name = self.class.name instance.instance_variables.each do |name| instance_variable_set name, instance.instance_variable_get(name) end super instance end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/theme/component.rb', line 64 def method_missing method, *args, &block # respond_to?(symbol, include_all=false) if instance.respond_to? method, true instance.send method, *args, &block else super end end |
Class Attribute Details
.html ⇒ Object (readonly)
Returns the value of attribute html.
25 26 27 |
# File 'lib/theme/component.rb', line 25 def html @html end |
.id(name) ⇒ Object (readonly)
Returns the value of attribute id.
25 26 27 |
# File 'lib/theme/component.rb', line 25 def id @id end |
.node ⇒ Object
Returns the value of attribute node.
26 27 28 |
# File 'lib/theme/component.rb', line 26 def node @node end |
.path ⇒ Object (readonly)
Returns the value of attribute path.
25 26 27 |
# File 'lib/theme/component.rb', line 25 def path @path end |
Instance Attribute Details
#instance ⇒ Object (readonly)
Returns the value of attribute instance.
10 11 12 |
# File 'lib/theme/component.rb', line 10 def instance @instance end |
#node ⇒ Object
Returns the value of attribute node.
62 63 64 |
# File 'lib/theme/component.rb', line 62 def node @node end |
Class Method Details
.clean(&block) ⇒ Object Also known as: setup
56 57 58 |
# File 'lib/theme/component.rb', line 56 def clean &block block.call node end |
.dom(location) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/theme/component.rb', line 43 def dom location node = Theme.cache.dom.fetch(path) do n = Nokogiri::HTML html Theme.cache.dom[path] = n end if location.is_a? String @node = node.at location else @node = node.send location.keys.first, location.values.last end end |
Instance Method Details
#render(meth = 'display', options = {}) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/theme/component.rb', line 77 def render meth = 'display', = {} if method(meth).parameters.length > 0 opts = Hashr.new() resp = send meth, opts else resp = send meth end .clear if resp.is_a? Nokogiri::XML::Element resp.to_html else resp end end |
#set_locals(options) ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/theme/component.rb', line 94 def set_locals .to_h.each do |key, value| (class << self; self; end).send(:attr_accessor, key.to_sym) instance_variable_set("@#{key}", value) end self end |