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_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)
Retrurns 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.
27 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 |
# File 'lib/extreml/type_element.rb', line 27 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
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/extreml/type_element.rb', line 97 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
70 71 72 |
# File 'lib/extreml/type_element.rb', line 70 def attributes @attributes end |
#content ⇒ Object Also known as: __content
80 81 82 83 84 85 86 |
# File 'lib/extreml/type_element.rb', line 80 def content if @content.nil? || @content.length > 1 return @content else return @content[0] end end |
#name ⇒ Object Also known as: __name
Getters
65 66 67 |
# File 'lib/extreml/type_element.rb', line 65 def name @name end |
#namespace ⇒ Object Also known as: __namespace
75 76 77 |
# File 'lib/extreml/type_element.rb', line 75 def namespace @namespace end |
#to_s ⇒ Object Also known as: __to_s
Override to_s to use in strings (useful for the last nesting level)
121 122 123 |
# File 'lib/extreml/type_element.rb', line 121 def to_s return self.__content end |
#to_xml(level = 0) ⇒ Object Also known as: __to_xml
Retrurns the document in XML format
127 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 |
# File 'lib/extreml/type_element.rb', line 127 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
160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/extreml/type_element.rb', line 160 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
89 90 91 92 |
# File 'lib/extreml/type_element.rb', line 89 def types # return self.methods - TypeElement.instance_methods return @types end |