Class: Theme::Component

Inherits:
SimpleDelegator
  • Object
show all
Includes:
Events
Defined in:
lib/theme/component.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

.htmlObject (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

.nodeObject

Returns the value of attribute node.



24
25
26
# File 'lib/theme/component.rb', line 24

def node
  @node
end

.pathObject (readonly)

Returns the value of attribute path.



23
24
25
# File 'lib/theme/component.rb', line 23

def path
  @path
end

Instance Attribute Details

#instanceObject (readonly)

Returns the value of attribute instance.



8
9
10
# File 'lib/theme/component.rb', line 8

def instance
  @instance
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/theme/component.rb', line 8

def name
  @name
end

#nodeObject

Returns the value of attribute node.



8
9
10
# File 'lib/theme/component.rb', line 8

def node
  @node
end

#tplObject

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

.tplObject



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', options = {}, &block
  if method(meth).parameters.length > 0
    opts = Hashr.new(options)
    resp = send meth, opts, &block
  else
    resp = send meth, &block
  end

  options.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 options = {}
  options.to_h.each do |key, value|
    (class << self; self; end).send(:attr_accessor, key.to_sym)
    instance_variable_set("@#{key}", value)
  end

  self
end