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.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#node ⇒ Object
Returns the value of attribute node.
-
#tpl ⇒ Object
Returns the value of attribute tpl.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(instance = false) ⇒ Component
constructor
A new instance of Component.
- #method_missing(method, *args, &block) ⇒ Object
- #partial(template, locals = {}) ⇒ Object
- #render(meth = 'display', options = {}, &block) ⇒ 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.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/theme/component.rb', line 10 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
66 67 68 69 70 71 72 73 |
# File 'lib/theme/component.rb', line 66 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.
23 24 25 |
# File 'lib/theme/component.rb', line 23 def html @html end |
.id(name) ⇒ Object (readonly)
Returns the value of attribute id.
23 24 25 |
# File 'lib/theme/component.rb', line 23 def id @id end |
.node ⇒ Object
Returns the value of attribute node.
24 25 26 |
# File 'lib/theme/component.rb', line 24 def node @node end |
.path ⇒ Object (readonly)
Returns the value of attribute path.
23 24 25 |
# File 'lib/theme/component.rb', line 23 def path @path end |
Instance Attribute Details
#instance ⇒ Object (readonly)
Returns the value of attribute instance.
8 9 10 |
# File 'lib/theme/component.rb', line 8 def instance @instance end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/theme/component.rb', line 8 def name @name end |
#node ⇒ Object
Returns the value of attribute node.
8 9 10 |
# File 'lib/theme/component.rb', line 8 def node @node end |
#tpl ⇒ Object
Returns the value of attribute tpl.
8 9 10 |
# File 'lib/theme/component.rb', line 8 def tpl @tpl end |
Class Method Details
.clean(&block) ⇒ Object Also known as: setup
58 59 60 |
# File 'lib/theme/component.rb', line 58 def clean &block block.call node end |
.dom(location) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/theme/component.rb', line 41 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 |
.src(path) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/theme/component.rb', line 31 def src path if path[/^\./] @path = path else @path = "#{Theme.config.path}/#{path}" end @html = Theme.load_file @path end |
.tpl ⇒ Object
54 55 56 |
# File 'lib/theme/component.rb', line 54 def tpl @tpl ||= OpenStruct.new end |
Instance Method Details
#partial(template, locals = {}) ⇒ Object
100 101 102 103 104 |
# File 'lib/theme/component.rb', line 100 def partial template, locals = {} locals[:partial] = template resp = render locals resp end |
#render(meth = 'display', options = {}, &block) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/theme/component.rb', line 83 def render meth = 'display', = {}, &block if method(meth).parameters.length > 0 opts = Hashr.new() resp = send meth, opts, &block else resp = send meth, &block end .clear if resp.is_a? Nokogiri::XML::Element resp.to_html else resp end end |
#set_locals(options = {}) ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/theme/component.rb', line 107 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 |