decontaminate 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 391a40ec6c400fea8dd7e0e0e236ccb54dd0d64c
4
- data.tar.gz: ebc11740db0cd3ca3aa55dddcd6854a5b1b21d34
3
+ metadata.gz: 29c7ecced8931be41ad5db4879c6387d734f7acf
4
+ data.tar.gz: bb3bf6fe5d28be34cbf82853ebb211aa52cf41b9
5
5
  SHA512:
6
- metadata.gz: 5ce173bd8e3aa615abc225a2cd5dc921fbff1e7ed0509d7dd485c5670f8d398acfbdefadef498bb9a0a6ec1b1d1666f9a5557cf856d042f4f2d452bd104d981e
7
- data.tar.gz: 151a004aafa09dfe7778e0d55ee5374a2d59ac6393f8ecba57e46ef27b573e64f38e780afb0afbb148f6599bd846f5954d3eee767ff234ac2b90741fef57a90b
6
+ metadata.gz: c675f357de093cef4949c21ad4456128ad6ed9464b408e5960a3676b515425b6bff5e9bb86b53537a3c5d33067001b79bbb52f37a7c875d76f0e0768218f5143
7
+ data.tar.gz: 1b8f16c664a0b38864f2b3511f8d3bf01f7d840b524ddbc0b36568dcb7a5e68540e15d187b843a1a61fb3c6b2bd0d34e24558e7dfbc43024869465d477f3b823
data/README.md CHANGED
@@ -85,6 +85,16 @@ end
85
85
 
86
86
  The `hash` method accepts a block, which works just like the class body, but all paths are scoped to the path passed to `hash`. The `key` argument is optional, just like with `scalar`.
87
87
 
88
+ Sometimes it may be useful to create an additional hash in the output as an organizational tool, even though there is no equivalent nesting in the input XML. In this case, the XPath argument may be omitted, specifying only `key:`.
89
+
90
+ ```ruby
91
+ hash key: 'info' do
92
+ scalar 'Email'
93
+ end
94
+ ```
95
+
96
+ This will fetch a value from the `Email` node on the root, but it will be stored in a property within a separate hash, keyed in the result with `'info'`.
97
+
88
98
  ### Array Data
89
99
 
90
100
  In addition to the `scalar` and `hash` methods, there are plural forms which allow parsing and extracting data that appears many times within a single document. These are named `scalars` and `hashes`, respectively. They work much like their singular counterparts, but the provided path should match multiple elements.
@@ -10,17 +10,17 @@ module Decontaminate
10
10
 
11
11
  def decode(xml_node)
12
12
  child = xml_node.at_xpath xpath
13
- text = coerce_node_to_text child
14
-
15
- return unless text
13
+ text = child && coerce_node_to_text(child)
16
14
 
17
15
  case type
18
16
  when :string
19
17
  text
20
18
  when :integer
21
- text.to_i
19
+ text && text.to_i
22
20
  when :float
23
- text.to_f
21
+ text && text.to_f
22
+ when :boolean
23
+ coerce_string_to_boolean text
24
24
  end
25
25
  end
26
26
 
@@ -33,6 +33,10 @@ module Decontaminate
33
33
  node.at_xpath('text()').to_s
34
34
  end
35
35
  end
36
+
37
+ def coerce_string_to_boolean(str)
38
+ str == 'true' || str == '1'
39
+ end
36
40
  end
37
41
  end
38
42
  end
@@ -35,7 +35,7 @@ module Decontaminate
35
35
  add_decoder key, decoder
36
36
  end
37
37
 
38
- def hash(xpath, key: infer_key(xpath), &body)
38
+ def hash(xpath = '.', key: infer_key(xpath), &body)
39
39
  decontaminator = Class.new(Decontaminate::Decontaminator, &body)
40
40
  add_decoder key, Decontaminate::Decoder::Hash.new(xpath, decontaminator)
41
41
  end
@@ -1,3 +1,3 @@
1
1
  module Decontaminate
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decontaminate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexis King