Class: Arbo::Element

Inherits:
Object
  • Object
show all
Includes:
BuilderMethods
Defined in:
lib/arbo/element.rb,
lib/arbo/element/proxy.rb,
lib/arbo/element/builder_methods.rb

Direct Known Subclasses

Context, HTML::Tag, HTML::TextNode

Defined Under Namespace

Modules: BuilderMethods Classes: Proxy

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BuilderMethods

#build_tag, #current_arbo_element, included, #insert_tag, #with_current_arbo_element

Constructor Details

#initialize(arbo_context = Arbo::Context.new) ⇒ Element

Returns a new instance of Element.



14
15
16
17
18
# File 'lib/arbo/element.rb', line 14

def initialize(arbo_context = Arbo::Context.new)
  @arbo_context = arbo_context
  @children = ElementCollection.new
  @parent = nil
end

Instance Attribute Details

#arbo_contextObject (readonly)

Returns the value of attribute arbo_context.



12
13
14
# File 'lib/arbo/element.rb', line 12

def arbo_context
  @arbo_context
end

#childrenObject (readonly)

Returns the value of attribute children.



12
13
14
# File 'lib/arbo/element.rb', line 12

def children
  @children
end

#parentObject

Returns the value of attribute parent.



11
12
13
# File 'lib/arbo/element.rb', line 11

def parent
  @parent
end

Instance Method Details

#+(element) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/arbo/element.rb', line 166

def +(element)
  case element
  when Element, ElementCollection
  else
    element = Arbo::HTML::TextNode.from_string(element)
  end
  to_ary + element
end

#<<(child) ⇒ Object



65
66
67
# File 'lib/arbo/element.rb', line 65

def <<(child)
  add_child(child)
end

#add_child(child) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/arbo/element.rb', line 37

def add_child(child)
  return unless child

  if child.is_a?(Array)
    child.each{|item| add_child(item) }
    return @children
  end

  # If its not an element, wrap it in a TextNode
  unless child.is_a?(Element)
    child = Arbo::HTML::TextNode.from_string(child)
  end

  if child.respond_to?(:parent)
    # Remove the child
    child.parent.remove_child(child) if child.parent && child.parent != self
    # Set ourselves as the parent
    child.parent = self
  end

  @children << child
end

#ancestorsObject



81
82
83
84
85
86
87
# File 'lib/arbo/element.rb', line 81

def ancestors
  if parent?
    [parent] + parent.ancestors
  else
    []
  end
end

#assignsObject



20
21
22
# File 'lib/arbo/element.rb', line 20

def assigns
  arbo_context.assigns
end

#build(*args, &block) ⇒ Object



32
33
34
35
# File 'lib/arbo/element.rb', line 32

def build(*args, &block)
  # Render the block passing ourselves in
  append_return_block(block.call(self)) if block
end

#children?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/arbo/element.rb', line 69

def children?
  @children.any?
end

#contentObject



119
120
121
# File 'lib/arbo/element.rb', line 119

def content
  children.to_s
end

#content=(contents) ⇒ Object



94
95
96
97
# File 'lib/arbo/element.rb', line 94

def content=(contents)
  clear_children!
  add_child(contents)
end

#each(&block) ⇒ Object



131
132
133
# File 'lib/arbo/element.rb', line 131

def each(&block)
  [to_s].each(&block)
end

#find_first_ancestor(type) ⇒ Object

TODO: Shouldn’t grab whole tree



90
91
92
# File 'lib/arbo/element.rb', line 90

def find_first_ancestor(type)
  ancestors.find{|a| a.is_a?(type) }
end

#get_elements_by_class_name(class_name) ⇒ Object Also known as: find_by_class



109
110
111
112
113
114
115
116
# File 'lib/arbo/element.rb', line 109

def get_elements_by_class_name(class_name)
  elements = ElementCollection.new
  children.each do |child|
    elements << child if child.class_list.include?(class_name)
    elements.concat(child.get_elements_by_class_name(class_name))
  end
  elements
end

#get_elements_by_tag_name(tag_name) ⇒ Object Also known as: find_by_tag



99
100
101
102
103
104
105
106
# File 'lib/arbo/element.rb', line 99

def get_elements_by_tag_name(tag_name)
  elements = ElementCollection.new
  children.each do |child|
    elements << child if child.tag_name == tag_name
    elements.concat(child.get_elements_by_tag_name(tag_name))
  end
  elements
end

#helpersObject



24
25
26
# File 'lib/arbo/element.rb', line 24

def helpers
  arbo_context.helpers
end

#html_safeObject



123
124
125
# File 'lib/arbo/element.rb', line 123

def html_safe
  to_s
end

#indent_levelObject



127
128
129
# File 'lib/arbo/element.rb', line 127

def indent_level
  parent? ? parent.indent_level + 1 : 0
end

#inspectObject



135
136
137
# File 'lib/arbo/element.rb', line 135

def inspect
  content
end

#parent?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/arbo/element.rb', line 77

def parent?
  !@parent.nil?
end

#remove_child(child) ⇒ Object



60
61
62
63
# File 'lib/arbo/element.rb', line 60

def remove_child(child)
  child.parent = nil if child.respond_to?(:parent=)
  @children.delete(child)
end

#render_in(context = arbo_context) ⇒ Object

Rendering strategy that visits all elements and appends output to a buffer.



150
151
152
153
154
# File 'lib/arbo/element.rb', line 150

def render_in(context = arbo_context)
  children.collect do |element|
    element.render_in_or_to_s(context)
  end.join('')
end

#render_in_or_to_s(context) ⇒ Object

Use render_in to render element unless closer ancestor overrides :to_s only.



157
158
159
160
161
162
163
164
# File 'lib/arbo/element.rb', line 157

def render_in_or_to_s(context)
  if method_distance(:render_in) <= method_distance(:to_s)
    render_in(context)
  else
    ActiveSupport::Deprecation.warn("#render_in should be defined for rendering #{method_owner(:to_s)} instead of #to_s")
    to_s.tap { |s| context.output_buffer << s }
  end
end

#tag_nameObject



28
29
30
# File 'lib/arbo/element.rb', line 28

def tag_name
  @tag_name ||= self.class.name.demodulize.downcase
end

#to_aryObject Also known as: to_a



175
176
177
# File 'lib/arbo/element.rb', line 175

def to_ary
  ElementCollection.new [Proxy.new(self)]
end

#to_sObject



144
145
146
147
# File 'lib/arbo/element.rb', line 144

def to_s
  ActiveSupport::Deprecation.warn("#render_in should be defined for rendering #{method_owner(:to_s)} instead of #to_s")
  content
end

#to_strObject



139
140
141
142
# File 'lib/arbo/element.rb', line 139

def to_str
  ActiveSupport::Deprecation.warn("don't rely on implicit conversion of Element to String")
  content
end