Class: Arbre::Element

Inherits:
Object
  • Object
show all
Includes:
BuilderMethods, Rails::Rendering
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 Rails::Rendering

#render

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.



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

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.



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

def arbre_context
  @arbre_context
end

#childrenObject (readonly)

Returns the value of attribute children.



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

def children
  @children
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

Instance Method Details

#+(element) ⇒ Object



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

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

#<<(child) ⇒ Object



66
67
68
# File 'lib/arbre/element.rb', line 66

def <<(child)
  add_child(child)
end

#add_child(child) ⇒ Object



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

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



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

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

#assignsObject



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

def assigns
  arbre_context.assigns
end

#build(*args, &block) ⇒ Object



33
34
35
36
# File 'lib/arbre/element.rb', line 33

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

#children?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/arbre/element.rb', line 70

def children?
  @children.any?
end

#contentObject



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

def content
  children.to_s
end

#content=(contents) ⇒ Object



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

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

#each(&block) ⇒ Object



132
133
134
# File 'lib/arbre/element.rb', line 132

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

#find_first_ancestor(type) ⇒ Object

TODO: Shouldn’t grab whole tree



91
92
93
# File 'lib/arbre/element.rb', line 91

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



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

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



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

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



25
26
27
# File 'lib/arbre/element.rb', line 25

def helpers
  arbre_context.helpers
end

#html_safeObject



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

def html_safe
  to_s
end

#indent_levelObject



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

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

#inspectObject



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

def inspect
  to_s
end

#parent?Boolean

Returns:

  • (Boolean)


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

def parent?
  !@parent.nil?
end

#remove_child(child) ⇒ Object



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

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

#tag_nameObject



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

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

#to_aryObject Also known as: to_a



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

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

#to_sObject



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

def to_s
  content
end

#to_strObject



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

def to_str
  to_s
end