Class: Extreml::TypeElement
- Inherits:
-
Object
- Object
- Extreml::TypeElement
- Defined in:
- lib/extreml/type_element.rb
Instance Method Summary collapse
- #add_new_type(name, content) ⇒ Object (also: #__add_new_type)
-
#add_type(name, content) ⇒ Object
(also: #__add_type)
Add a type method, that returns an array if there are more elements with the same tag, a Type object if it’s just one, or the content if it’s the last level.
- #attributes ⇒ Object (also: #__attributes)
- #content ⇒ Object (also: #__content)
-
#initialize(document = nil, main_element: nil) ⇒ TypeElement
constructor
A new instance of TypeElement.
-
#name ⇒ Object
(also: #__name)
Getters.
- #namespace ⇒ Object (also: #__namespace)
-
#to_hash ⇒ Object
(also: #__to_hash)
Returns a hash of the document.
-
#to_json ⇒ Object
(also: #__to_json)
Returns the document in JSON format.
-
#to_s ⇒ Object
(also: #__to_s)
Override to_s to use in strings (useful for the last nesting level).
-
#to_xml(level = 0) ⇒ Object
(also: #__to_xml)
Returns the document in XML format.
-
#tree(level: 0, attributes: false) ⇒ Object
(also: #__tree)
This method is for debug purposes only, it prints a tree of the document.
- #types ⇒ Object (also: #__types)
-
#update_content(name, content) ⇒ Object
(also: #__update_content)
update content from a subsequent element recursively call update on main_element.
Constructor Details
#initialize(document = nil, main_element: nil) ⇒ TypeElement
Returns a new instance of TypeElement.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/extreml/type_element.rb', line 28 def initialize document = nil, main_element: nil # document model: # # { # name: 'string', # namespace: 'string'|nil, # attributes: [attributes_array]|nil, # content: [mixed_array]|nil # } # # attributes model: # # { # property: 'string', # namespace: 'string'|nil, # value: 'string' # } # # Initialize properties @name = document[:name] @namespace = document[:namespace] @attributes = document[:attributes] @content = document[:content] @types = Array.new @main_element = main_element # Add a type for every element in content unless @content.nil? @content.each do |v| if v.class == Hash __add_type v[:name], v end end end end |
Instance Method Details
#add_new_type(name, content) ⇒ Object Also known as: __add_new_type
141 142 143 144 145 146 |
# File 'lib/extreml/type_element.rb', line 141 def add_new_type name, content self.__add_type name, {name: name, namespace: nil, attributes: [], content: [content].flatten} self.__update_content name, content end |
#add_type(name, content) ⇒ Object Also known as: __add_type
Add a type method, that returns an array if there are more elements with the same tag, a Type object if it’s just one, or the content if it’s the last level
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/extreml/type_element.rb', line 99 def add_type name, content # If method exists, override it and return an array including all previous elements # Else create a method that returns a new object of the content if self.__types.any? name.to_sym array = self.send name.to_sym define_singleton_method name.to_sym do content = [array].flatten + [(content.class == Hash ? (Extreml::TypeElement.new content, main_element: self) : content)] case content.length when 0 return nil when 1 return content[0] else return content end end else define_singleton_method name.to_sym do if content.class == Hash return Extreml::TypeElement.new content, main_element: self else if content.class == Array case content.length when 0 return nil when 1 return content[0] else return content end else return content end end end @types << name.to_sym end end |
#attributes ⇒ Object Also known as: __attributes
72 73 74 |
# File 'lib/extreml/type_element.rb', line 72 def attributes @attributes end |
#content ⇒ Object Also known as: __content
82 83 84 85 86 87 88 |
# File 'lib/extreml/type_element.rb', line 82 def content if @content.nil? || @content.length > 1 return @content else return @content[0] end end |
#name ⇒ Object Also known as: __name
Getters
67 68 69 |
# File 'lib/extreml/type_element.rb', line 67 def name @name end |
#namespace ⇒ Object Also known as: __namespace
77 78 79 |
# File 'lib/extreml/type_element.rb', line 77 def namespace @namespace end |
#to_hash ⇒ Object Also known as: __to_hash
Returns a hash of the document
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/extreml/type_element.rb', line 208 def to_hash hash = Hash.new hash = { namespace: @namespace, attributes: @attributes } if @types.empty? hash[:content] = @content else @types.each do |t| obj = self.send(t) if obj.class == Array hash[t] = Array.new obj.each do |o| hash[t] << o.to_hash end else if obj.class == Extreml::TypeElement hash[t] = obj.to_hash else hash[t] = obj end end end end return hash end |
#to_json ⇒ Object Also known as: __to_json
Returns the document in JSON format
202 203 204 |
# File 'lib/extreml/type_element.rb', line 202 def to_json return self.to_hash.to_json end |
#to_s ⇒ Object Also known as: __to_s
Override to_s to use in strings (useful for the last nesting level)
163 164 165 |
# File 'lib/extreml/type_element.rb', line 163 def to_s return self.__content end |
#to_xml(level = 0) ⇒ Object Also known as: __to_xml
Returns the document in XML format
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/extreml/type_element.rb', line 169 def to_xml level = 0 xml = '' xml += "#{' ' * level}<#{@namespace.nil? ? '' : "#{@namespace}:"}#{@name}" unless @attributes.nil? @attributes.each do |a| xml += " #{a[:namespace].nil? ? '' : "#{a[:namespace]}:"}#{a[:property]}=\"#{a[:value]}\"" end end if @content.nil? xml += "/>" else xml += ">" if @types.empty? xml += "#{@content.join}" else @types.each do |t| content = self.send(t) if content.class == Array content.each do |c| xml += "\n#{c.to_xml (level + 1)}" end else xml += "\n#{content.to_xml (level + 1)}" end end end xml += "#{@types.empty? ? '' : "\n#{' ' * level}"}</#{@namespace.nil? ? '' : "#{@namespace}:"}#{@name}>" end return xml end |
#tree(level: 0, attributes: false) ⇒ Object Also known as: __tree
This method is for debug purposes only, it prints a tree of the document
238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/extreml/type_element.rb', line 238 def tree level: 0, attributes: false pre = level > 0 ? "#{' ' * level}|#{'-->'}" : "" puts "#{pre}#{self.__namespace}:#{self.__name} #{self.__types.inspect}" + (attributes ? " #{self.__attributes}" : "") level += 1 self.__types.each do |m| next_type = self.send(m) [next_type].flatten.each do |nt| (nt.__tree level: level, attributes: attributes) unless next_type.nil? end end end |
#types ⇒ Object Also known as: __types
91 92 93 94 |
# File 'lib/extreml/type_element.rb', line 91 def types # return self.methods - TypeElement.instance_methods return @types end |
#update_content(name, content) ⇒ Object Also known as: __update_content
update content from a subsequent element recursively call update on main_element
151 152 153 154 155 156 157 158 159 |
# File 'lib/extreml/type_element.rb', line 151 def update_content name, content @content << {name: name, namespace: nil, attributes: [], content: [content].flatten} unless @main_element.nil? @main_element.update_content @name, self end end |