Class: OXM::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/oxm/object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, attrs = {}) ⇒ OXM::Object

Parameters:

  • tag (String)
  • attrs (Hash) (defaults to: {})


125
126
127
128
129
130
131
# File 'lib/oxm/object.rb', line 125

def initialize tag, attrs = {}
  @tag = tag
  @attrs = attrs
  @nodes = {}
  @cdata = false
  @data  = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symb, *args) ⇒ Object (private)



139
140
141
142
143
144
145
# File 'lib/oxm/object.rb', line 139

def method_missing(symb, *args)
  if @nodes.has_key?(symb.to_s)
    return @nodes[symb.to_s]
  else
    raise NoMethodError.new("undefined method or attribute `#{symb}'")
  end
end

Instance Attribute Details

#tagObject (readonly)

Returns the value of attribute tag.



3
4
5
# File 'lib/oxm/object.rb', line 3

def tag
  @tag
end

Instance Method Details

#[](attr) ⇒ String

Parameters:

  • attr (String)

Returns:

  • (String)


70
71
72
# File 'lib/oxm/object.rb', line 70

def [] attr
  @attrs[attr.to_s]
end

#[]=(attr, val) ⇒ String

Parameters:

  • attr (String)
  • val (String)

Returns:

  • (String)


77
78
79
# File 'lib/oxm/object.rb', line 77

def []= attr, val
  @attrs[attr.to_s] = val
end

#add_node(tag, object) ⇒ OXM::Object

Parameters:

Returns:



95
96
97
98
99
# File 'lib/oxm/object.rb', line 95

def add_node tag, object
  @nodes[tag] ||= []
  @nodes[tag] << object
  self
end

#attributesHash

Returns:

  • (Hash)


82
83
84
# File 'lib/oxm/object.rb', line 82

def attributes
  @attrs
end

#cdata=(val) ⇒ String

Parameters:

  • val (String)

    CDATA content

Returns:

  • (String)


20
21
22
23
# File 'lib/oxm/object.rb', line 20

def cdata= val
  @cdata = true
  @data = val
end

#cdata?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/oxm/object.rb', line 26

def cdata?
  @cdata
end

#compact!OXM::Object

Compacts the object by removing empty child elements and then collapsing element Arrays. After compaction, single-element arrays are collapsed, and empty arrays become nil.

Returns:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/oxm/object.rb', line 104

def compact!
  @nodes.each do |key, value|
    if value.is_a?(Array)
      value = value.reject(&:empty?)
      @nodes[key] =
        case value.length
        when 0
          nil
        when 1
          value.first
        else
          value
        end
    end
  end
  self
end

#contentString/NilClass

Returns:

  • (String/NilClass)


6
7
8
# File 'lib/oxm/object.rb', line 6

def content
  @data
end

#content=(val) ⇒ String

Parameters:

  • val (String)

    Text

  • val (String)

Returns:

  • (String)


13
14
15
16
# File 'lib/oxm/object.rb', line 13

def content= val
  @cdata = false
  @data = val
end

#elementsHash Also known as: children

Returns:

  • (Hash)


87
88
89
# File 'lib/oxm/object.rb', line 87

def elements
  @nodes
end

#empty?Boolean

Return if the element is empty (no text value, no attributes and no child elements)

Returns:

  • (Boolean)

    Return if the element is empty (no text value, no attributes and no child elements)



134
135
136
# File 'lib/oxm/object.rb', line 134

def empty?
  self.to_s.empty? && self.attributes.empty? && self.elements.empty?
end

#inspectString

Returns:

  • (String)


64
65
66
# File 'lib/oxm/object.rb', line 64

def inspect
  to_xml
end

#to_sString

Returns:

  • (String)


59
60
61
# File 'lib/oxm/object.rb', line 59

def to_s
  @data.to_s
end

#to_xmlString

Returns XML expression for this object.

Returns:

  • (String)

    XML expression for this object



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
# File 'lib/oxm/object.rb', line 31

def to_xml
  io = StringIO.new
  builder = Builder::XmlMarkup.new(:target => io)

  build_node = lambda do |node|
    builder.tag!(node.tag, node.attributes) do
      node.elements.each do |tag, elements|
        next if elements.nil?
        elements = [elements] unless elements.is_a? Array
        elements.each do |child|
          build_node.call child
        end
      end
      if node.content
        if node.cdata?
          builder.cdata! node.to_s
        else
          builder.text! node.to_s
        end
      end
    end
  end

  build_node.call self
  io.string
end