Class: TypeElement

Inherits:
Object
  • Object
show all
Defined in:
lib/extreml/type_element.rb

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ 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 ? (TypeElement.new content) : content)]
    end
  else
    define_singleton_method name.to_sym do
      if content.class == Hash
        return TypeElement.new content
      else
        return content
      end
    end
    @types << name.to_sym
  end

end

#attributesObject Also known as: __attributes



70
71
72
# File 'lib/extreml/type_element.rb', line 70

def attributes
  @attributes
end

#contentObject 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

#nameObject Also known as: __name

Getters



65
66
67
# File 'lib/extreml/type_element.rb', line 65

def name
  @name
end

#namespaceObject Also known as: __namespace



75
76
77
# File 'lib/extreml/type_element.rb', line 75

def namespace
  @namespace
end

#to_sObject 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

#tree(level: 0, attributes: false) ⇒ Object Also known as: __tree

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



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/extreml/type_element.rb', line 127

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

#typesObject 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