Class: Arbre::Element

Inherits:
Object
  • Object
show all
Includes:
BuilderMethods
Defined in:
lib/arbre/element.rb,
lib/arbre/element/proxy.rb,
lib/arbre/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_arbre_element, included, #insert_tag, #with_current_arbre_element

Constructor Details

#initialize(arbre_context = Arbre::Context.new) ⇒ Element

Returns a new instance of Element.


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

def initialize(arbre_context = Arbre::Context.new)
  @arbre_context = arbre_context
  @children = ElementCollection.new
  @parent = nil
end

Instance Attribute Details

#arbre_contextObject (readonly)

Returns the value of attribute arbre_context


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

def arbre_context
  @arbre_context
end

#childrenObject (readonly)

Returns the value of attribute children


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

def children
  @children
end

#parentObject

Returns the value of attribute parent


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

def parent
  @parent
end

Instance Method Details

#+(element) ⇒ Object


147
148
149
150
151
152
153
154
# File 'lib/arbre/element.rb', line 147

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

#<<(child) ⇒ Object


65
66
67
# File 'lib/arbre/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/arbre/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 = Arbre::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/arbre/element.rb', line 81

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

#assignsObject


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

def assigns
  arbre_context.assigns
end

#build(*args, &block) ⇒ Object


32
33
34
35
# File 'lib/arbre/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/arbre/element.rb', line 69

def children?
  @children.any?
end

#contentObject


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

def content
  children.to_s
end

#content=(contents) ⇒ Object


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

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

#each(&block) ⇒ Object


131
132
133
# File 'lib/arbre/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/arbre/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/arbre/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/arbre/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/arbre/element.rb', line 24

def helpers
  arbre_context.helpers
end

#html_safeObject


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

def html_safe
  to_s
end

#indent_levelObject


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

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

#inspectObject


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

def inspect
  to_s
end

#parent?Boolean

Returns:

  • (Boolean)

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

def parent?
  !@parent.nil?
end

#remove_child(child) ⇒ Object


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

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

#tag_nameObject


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

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

#to_aryObject Also known as: to_a


156
157
158
# File 'lib/arbre/element.rb', line 156

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

#to_sObject


143
144
145
# File 'lib/arbre/element.rb', line 143

def to_s
  content
end

#to_strObject


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

def to_str
  to_s
end