class Extreml::TypeElement

Public Class Methods

new(document = nil, main_element: nil) click to toggle source
# 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

Public Instance Methods

__add_new_type(name, content)
Alias for: add_new_type
__add_type(name, content)
Alias for: add_type
__attributes()
Alias for: attributes
__content()
Alias for: content
__name()
Alias for: name
__namespace()
Alias for: namespace
__to_hash()
Alias for: to_hash
__to_json()
Alias for: to_json
__to_s()
Alias for: to_s
__to_xml(level = 0)
Alias for: to_xml
__tree(level: 0, attributes: false)
Alias for: tree
__types()
Alias for: types
__update_content(name, content)
Alias for: update_content
add_new_type(name, content) click to toggle source
# 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
Also aliased as: __add_new_type
add_type(name, content) click to toggle source

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

# 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
Also aliased as: __add_type
attributes() click to toggle source
# File lib/extreml/type_element.rb, line 72
def attributes
  @attributes
end
Also aliased as: __attributes
content() click to toggle source
# File lib/extreml/type_element.rb, line 82
def content
  if @content.nil? || @content.length >  1
    return @content
  else
    return @content[0]
  end
end
Also aliased as: __content
name() click to toggle source

Getters

# File lib/extreml/type_element.rb, line 67
def name
  @name
end
Also aliased as: __name
namespace() click to toggle source
# File lib/extreml/type_element.rb, line 77
def namespace
  @namespace
end
Also aliased as: __namespace
to_hash() click to toggle source

Returns a hash of the document

# 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
Also aliased as: __to_hash
to_json() click to toggle source

Returns the document in JSON format

# File lib/extreml/type_element.rb, line 202
def to_json
  return self.to_hash.to_json
end
Also aliased as: __to_json
to_s() click to toggle source

Override to_s to use in strings (useful for the last nesting level)

# File lib/extreml/type_element.rb, line 163
def to_s
  return self.__content
end
Also aliased as: __to_s
to_xml(level = 0) click to toggle source

Returns the document in XML format

# 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
Also aliased as: __to_xml
tree(level: 0, attributes: false) click to toggle source

This method is for debug purposes only, it prints a tree of the document

# 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
Also aliased as: __tree
types() click to toggle source
# File lib/extreml/type_element.rb, line 91
def types
  # return self.methods - TypeElement.instance_methods
  return @types
end
Also aliased as: __types
update_content(name, content) click to toggle source

update content from a subsequent element recursively call update on main_element

# 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
Also aliased as: __update_content