Class: Extreml::TypeElement
- Inherits:
-
Object
- Object
- Extreml::TypeElement
- Defined in:
- lib/extreml/type_element.rb
Instance Method Summary collapse
-
#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) ⇒ 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)
Constructor Details
#initialize(document) ⇒ 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 |
# File 'lib/extreml/type_element.rb', line 28 def initialize document # 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 # 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_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
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/extreml/type_element.rb', line 98 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 return [array].flatten + [(content.class == Hash ? (Extreml::TypeElement.new content) : content)] end else define_singleton_method name.to_sym do if content.class == Hash return Extreml::TypeElement.new content else return content end end @types << name.to_sym end end |
#attributes ⇒ Object Also known as: __attributes
71 72 73 |
# File 'lib/extreml/type_element.rb', line 71 def attributes @attributes end |
#content ⇒ Object Also known as: __content
81 82 83 84 85 86 87 |
# File 'lib/extreml/type_element.rb', line 81 def content if @content.nil? || @content.length > 1 return @content else return @content[0] end end |
#name ⇒ Object Also known as: __name
Getters
66 67 68 |
# File 'lib/extreml/type_element.rb', line 66 def name @name end |
#namespace ⇒ Object Also known as: __namespace
76 77 78 |
# File 'lib/extreml/type_element.rb', line 76 def namespace @namespace end |
#to_hash ⇒ Object Also known as: __to_hash
Returns a hash of the document
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/extreml/type_element.rb', line 167 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 hash[t] = obj.to_hash end end end return hash end |
#to_json ⇒ Object Also known as: __to_json
Returns the document in JSON format
161 162 163 |
# File 'lib/extreml/type_element.rb', line 161 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)
122 123 124 |
# File 'lib/extreml/type_element.rb', line 122 def to_s return self.__content end |
#to_xml(level = 0) ⇒ Object Also known as: __to_xml
Returns the document in XML format
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/extreml/type_element.rb', line 128 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
193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/extreml/type_element.rb', line 193 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
90 91 92 93 |
# File 'lib/extreml/type_element.rb', line 90 def types # return self.methods - TypeElement.instance_methods return @types end |