Class: ObjectView::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/object_view/element.rb

Direct Known Subclasses

Body, Div, Head, Header, Javascript, JavascriptFile, Li, Link, Page, Span, TBody, TCell, THead, TRow, Table, Ul

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeElement

Returns a new instance of Element.



9
10
11
12
13
14
15
16
# File 'lib/object_view/element.rb', line 9

def initialize
  @tab = "  "
  @single_line = false
  @tag = nil
  @attributes = Hash.new
  @children = []
  @acceptable_children = [String, Element]
end

Instance Attribute Details

#acceptable_childrenObject

Returns the value of attribute acceptable_children.



7
8
9
# File 'lib/object_view/element.rb', line 7

def acceptable_children
  @acceptable_children
end

#attributesObject

Returns the value of attribute attributes.



6
7
8
# File 'lib/object_view/element.rb', line 6

def attributes
  @attributes
end

#childrenObject (readonly)

Returns the value of attribute children.



7
8
9
# File 'lib/object_view/element.rb', line 7

def children
  @children
end

#single_lineObject

Returns the value of attribute single_line.



6
7
8
# File 'lib/object_view/element.rb', line 6

def single_line
  @single_line
end

#tagObject

Returns the value of attribute tag.



6
7
8
# File 'lib/object_view/element.rb', line 6

def tag
  @tag
end

Instance Method Details

#<<(item) ⇒ Object



92
93
94
# File 'lib/object_view/element.rb', line 92

def <<(item)
  self.add item
end

#add(element_or_string) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/object_view/element.rb', line 68

def add(element_or_string)
  if (self.is_acceptable_child?(element_or_string))
    @children << element_or_string
  elsif (element_or_string.is_a?(Array))
    element_or_string.each do |child|
      add child
    end
  else
    raise IllegalChildElementError.new "Parent: #{self.tag} - Attempt to add an element that is not a valid child, and not an ObjectView::Element.  Class of element: #{element_or_string.class.name}:  #{element_or_string}\nTag of parent: #{self.tag}.\nAcceptable children: #{@acceptable_children.join(',')}"
  end
  return element_or_string
end

#add_with_tag(tag, content = nil) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/object_view/element.rb', line 81

def add_with_tag(tag, content = nil)
  element = Element.new
  element.tag = tag
  if (content != nil)
    element.children << content
  end
  element.single_line = false
  self.children << element
  return element
end

#attr(name, value) ⇒ Object



35
36
37
# File 'lib/object_view/element.rb', line 35

def attr(name, value)
  @attributes[name] = value
end

#css_class=(value) ⇒ Object



51
52
53
# File 'lib/object_view/element.rb', line 51

def css_class=(value)
  self.attr("class", value)
end

#find_element_with_tag(tag) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/object_view/element.rb', line 19

def find_element_with_tag(tag)
  found = []
  @children.each do |child|
    if (child.is_a?(Element)) && (child.tag == tag)
      found << child
    end
  end
  if (found.length == 0)
    return nil
  elsif (found.length == 1)
    return found[0]
  else
    return found
  end
end

#id=(value) ⇒ Object



55
56
57
# File 'lib/object_view/element.rb', line 55

def id=(value)
  self.attr("id", value)
end

#is_acceptable_child?(child) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
# File 'lib/object_view/element.rb', line 59

def is_acceptable_child?(child)
  @acceptable_children.each do |good_class|
    if (child.is_a?(good_class)) 
      return true
    end
  end
  return false
end

#on_click=(value) ⇒ Object



43
44
45
# File 'lib/object_view/element.rb', line 43

def on_click=(value)
  self.attr("onclick", value)
end

#render(indent = 0) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/object_view/element.rb', line 136

def render(indent = 0)
  raise "tag not defined for class: #{self.class}" if (tag == nil) || (tag.strip.length == 0)
  html = StringIO.new
  if (indent != nil)
    html << (@tab * indent) 
  end
  html << "<#{tag}#{render_attributes}>"
  if (self.single_line)
    html << "#{render_children(nil)}" 
  else
    if (indent == nil)
      html <<  render_children(indent)
    else
      html <<  render_children(indent + 1)
    end
  end


  if (!self.single_line)
    html << "\n"
    if (indent != nil)
      html << @tab * indent 
    end
  end
  
  html << "</#{tag}>\n"
  return html.string
end

#render_attributesObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/object_view/element.rb', line 115

def render_attributes
  html = StringIO.new
  if (@attributes.length > 0)
    @attributes.each do |name, value|
      if (value.class == Hash)
        attr_value = ''
        value.keys.each do |key|
          attr_value += "#{key.to_s.gsub('_', '-')}: #{value[key]}"
          if (key != value.keys.last)
            attr_value += "; "
          end
        end
        html << " #{name}=\"#{attr_value}\""
      else
        html << " #{name}=\"#{value}\""
      end
    end
  end
  return html.string
end

#render_children(indent = 0) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/object_view/element.rb', line 96

def render_children(indent = 0)
  html = StringIO.new
  @children.each do |child|
    if (indent != nil)
      if (!self.single_line)
        html << "\n"
      end
      html << @tab * indent
    end
    if (child.class.name == "String")
      html << child
    else
      child_html = child.render(indent)
      html << child_html
    end
  end
  return html.string
end

#style=(the_style) ⇒ Object



47
48
49
# File 'lib/object_view/element.rb', line 47

def style=(the_style)
  self.attr("style", the_style)
end