troelskn-handsoap 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 3
3
- :patch: 0
3
+ :patch: 1
4
4
  :major: 0
@@ -62,6 +62,36 @@ module Handsoap
62
62
  end
63
63
  end
64
64
 
65
+ # NodeSelection is a wrapper around Array, that implicitly delegates BaseDriver methods to the first element.
66
+ #
67
+ # It makes mapping code prettier, since you often need to access the first element of a selection.
68
+ class NodeSelection < Array
69
+ def to_i
70
+ self.first.to_i if self.any?
71
+ end
72
+ def to_f
73
+ self.first.to_f if self.any?
74
+ end
75
+ def to_boolean
76
+ self.first.to_boolean if self.any?
77
+ end
78
+ def to_date
79
+ self.first.to_date if self.any?
80
+ end
81
+ def node_name
82
+ self.first.node_name if self.any?
83
+ end
84
+ def xpath(expression, ns = nil)
85
+ self.first.xpath(expression, ns)
86
+ end
87
+ def to_s
88
+ self.first.to_s if self.any?
89
+ end
90
+ def to_xml
91
+ self.first.to_xml if self.any?
92
+ end
93
+ end
94
+
65
95
  # Wraps the underlying (native) xml driver, and provides a uniform interface.
66
96
  module BaseDriver
67
97
  def initialize(element, namespaces = {})
@@ -142,9 +172,17 @@ module Handsoap
142
172
  def to_xml
143
173
  raise NotImplementedError.new
144
174
  end
145
- # Alias for +xpath+
175
+ # Calls +xpath+ and wraps the result in a +NodeSelection+.
146
176
  def /(expression)
147
- self.xpath(expression)
177
+ NodeSelection.new self.xpath(expression)
178
+ end
179
+ # Returns the attribute value of the underlying element.
180
+ #
181
+ # Shortcut for:
182
+ #
183
+ # (node/"@attribute_name").to_s
184
+ def [](attribute_name)
185
+ raise NotImplementedError.new
148
186
  end
149
187
  end
150
188
 
@@ -162,6 +200,10 @@ module Handsoap
162
200
  assert_prefixes!(expression, ns)
163
201
  @element.find(expression, ns.map{|k,v| "#{k}:#{v}" }).to_a.map{|node| LibXMLDriver.new(node, ns) }
164
202
  end
203
+ def [](attribute_name)
204
+ raise ArgumentError.new unless attribute_name.kind_of? String
205
+ @element[attribute_name]
206
+ end
165
207
  def to_xml
166
208
  @element.to_s
167
209
  end
@@ -188,6 +230,10 @@ module Handsoap
188
230
  assert_prefixes!(expression, ns)
189
231
  REXML::XPath.match(@element, expression, ns).map{|node| REXMLDriver.new(node, ns) }
190
232
  end
233
+ def [](attribute_name)
234
+ raise ArgumentError.new unless attribute_name.kind_of? String
235
+ @element.attributes[attribute_name]
236
+ end
191
237
  def to_xml
192
238
  require 'rexml/formatters/pretty'
193
239
  formatter = REXML::Formatters::Pretty.new
@@ -225,6 +271,10 @@ module Handsoap
225
271
  assert_prefixes!(expression, ns)
226
272
  @element.xpath(expression, ns).map{|node| NokogiriDriver.new(node, ns) }
227
273
  end
274
+ def [](attribute_name)
275
+ raise ArgumentError.new unless attribute_name.kind_of? String
276
+ @element[attribute_name]
277
+ end
228
278
  def to_xml
229
279
  @element.serialize(NokogiriDriver.serialize_args)
230
280
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: troelskn-handsoap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Troels Knak-Nielsen