ox 1.5.9 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of ox might be problematic. Click here for more details.

Files changed (4) hide show
  1. data/README.md +2 -6
  2. data/lib/ox/element.rb +57 -0
  3. data/lib/ox/version.rb +1 -1
  4. metadata +2 -2
data/README.md CHANGED
@@ -34,13 +34,9 @@ A fast XML parser and Object marshaller as a Ruby gem.
34
34
 
35
35
  ## <a name="release">Release Notes</a>
36
36
 
37
- ### Release 1.5.9
37
+ ### Release 1.6.0
38
38
 
39
- - Merged a fix by edanaher that allows special characters in attribute values
40
-
41
- - Changed all comments to old C style to get code to compile with Ruby 2.0.0.
42
-
43
- - Other changes to time handling for 2.0.0.
39
+ - Ox::Elements now support and 'easy' API. Elements now return elements by calling a method that matches the name of the child element or attribute. Check out the documentation on the 'easy' API at [http://www.ohler.com/ox/Ox/Element.html](http://www.ohler.com/ox/Ox/Element.html).
44
40
 
45
41
  ## <a name="description">Description</a>
46
42
 
@@ -3,6 +3,37 @@ module Ox
3
3
 
4
4
  # An Element represents a element of an XML document. It has a name,
5
5
  # attributes, and sub-nodes.
6
+ #
7
+ # To access the child elements or attributes there are several options. One
8
+ # is to walk the nodes and attributes. Another is to use the locate()
9
+ # method. The easiest for simple regularly formatted XML is to reference the
10
+ # sub elements or attributes simply by name. Repeating elements with the
11
+ # same name can be referenced with an element count as well. A few examples
12
+ # should explain the 'easy' API more clearly.
13
+ #
14
+ # *Example*
15
+ #
16
+ # doc = Ox.parse(%{
17
+ # <?xml?>
18
+ # <People>
19
+ # <Person age="58">
20
+ # <given>Peter</given>
21
+ # <surname>Ohler</surname>
22
+ # </Person>
23
+ # <Person>
24
+ # <given>Makie</given>
25
+ # <surname>Ohler</surname>
26
+ # </Person>
27
+ # </People>
28
+ # })
29
+ #
30
+ # doc.People.Person.given.text
31
+ # => "Peter"
32
+ # doc.People.Person(1).given.text
33
+ # => "Makie"
34
+ # doc.People.Person.age
35
+ # => "58"
36
+
6
37
  class Element < Node
7
38
 
8
39
  # Creates a new Element with the specified name.
@@ -66,6 +97,13 @@ module Ox
66
97
  end
67
98
  alias == eql?
68
99
 
100
+ # Returns the first String in the elements nodes array or nil if there is
101
+ # no String node.
102
+ def text()
103
+ @nodes.each { |n| return n if n.is_a?(String) }
104
+ nil
105
+ end
106
+
69
107
  # Returns an array of Nodes or Strings that correspond to the locations
70
108
  # specified by the path parameter. The path parameter describes the path
71
109
  # to the return values which can be either nodes in the XML or
@@ -109,6 +147,25 @@ module Ox
109
147
  found
110
148
  end
111
149
 
150
+ # Handles the 'easy' API that allows navigating a simple XML by
151
+ # referencing elements and attributes by name.
152
+ # @param [Symbol] id element or attribute name
153
+ # @return [Element|Node|String|nil] the element, attribute value, or Node identifed by the name
154
+ # @raise [NoMethodError] if no match is found
155
+ def method_missing(id, *args, &block)
156
+ ids = id.to_s
157
+ i = args[0].to_i # will be 0 if no arg or parsing fails
158
+ @nodes.each do |n|
159
+ if n.is_a?(Element) && (n.value == id || n.value == ids)
160
+ return n if 0 == i
161
+ i -= 1
162
+ end
163
+ end
164
+ return @attributes[id] if @attributes.has_key?(id)
165
+ return @attributes[ids] if @attributes.has_key?(ids)
166
+ raise NoMethodError.new("#{name} not found", name)
167
+ end
168
+
112
169
  # @param [Array] path array of steps in a path
113
170
  # @param [Array] found matching nodes
114
171
  def alocate(path, found)
@@ -1,5 +1,5 @@
1
1
 
2
2
  module Ox
3
3
  # Current version of the module.
4
- VERSION = '1.5.9'
4
+ VERSION = '1.6.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ox
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.9
4
+ version: 1.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-07 00:00:00.000000000 Z
12
+ date: 2012-09-23 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! "A fast XML parser and object serializer that uses only standard C
15
15
  lib.\n \nOptimized XML (Ox), as the name implies was written to provide