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.



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

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

.nodeObject

Returns the value of attribute node.



26
27
28
# File 'lib/theme/component.rb', line 26

def node
  @node
end

.pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Instance Attribute Details

#instanceObject (readonly)

Returns the value of attribute instance.



10
11
12
# File 'lib/theme/component.rb', line 10

def instance
  @instance
end

#nodeObject

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

.src(path) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/theme/component.rb', line 33

def src path
  if path[/^\./]
    @path = path
  else
    @path = "#{Theme.config.path}/#{path}"
  end

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

  options.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 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