omnis 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,7 @@ require "omnis/version"
2
2
 
3
3
  require 'active_support/core_ext/hash'
4
4
  require 'omnis/nested_hash_extractor'
5
+ require 'omnis/monadic_nested_hash_extractor'
5
6
  require 'omnis/transformer'
6
7
  require 'omnis/operators'
7
8
  require 'omnis/query'
@@ -0,0 +1,11 @@
1
+ require 'monadic'
2
+
3
+ module Omnis
4
+ class MonadicNestedHashExtractor < NestedHashExtractor
5
+ def extractor(path)
6
+ raise ArgumentError("path to extract must be a string") unless String === path
7
+ expr = "source#{from_dot_path(path)} rescue Nothing"
8
+ ->source { Maybe(eval(expr)) }
9
+ end
10
+ end
11
+ end
@@ -1,11 +1,9 @@
1
- require 'monadic'
2
-
3
1
  module Omnis
4
2
  class NestedHashExtractor
5
3
  # returns a lambda which extracts a value from a nested hash
6
4
  def extractor(path)
7
5
  raise ArgumentError("path to extract must be a string") unless String === path
8
- expr = "source#{from_dot_path(path)} rescue Nothing"
6
+ expr = "source#{from_dot_path(path)} rescue nil"
9
7
  ->source { eval(expr) }
10
8
  end
11
9
 
@@ -18,7 +16,7 @@ module Omnis
18
16
 
19
17
  def field(f)
20
18
  return '[' << f << ']' if is_i?(f)
21
- return '.fetch("' << f << '", Nothing)'
19
+ return '.fetch("' << f << '", nil)'
22
20
  end
23
21
 
24
22
  # checks if the string is a number
@@ -1,3 +1,3 @@
1
1
  module Omnis
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+ require 'omnis/nested_hash_extractor'
3
+ require 'omnis/monadic_nested_hash_extractor'
4
+
5
+ describe Omnis::MonadicNestedHashExtractor do
6
+ let(:xtr) { Omnis::MonadicNestedHashExtractor.new }
7
+ let(:doc) {
8
+ { "ref_anixe" => "1abc",
9
+ "contract" => "test",
10
+ "agency" => nil,
11
+ "services" => [ { "date_from" => "2012-10-10"}]}
12
+ }
13
+
14
+ it "extracts values using a path" do
15
+ xtr.extractor("ref_anixe").call(doc).should == Just("1abc")
16
+ xtr.extractor("services.0.date_from").call(doc).should == Just("2012-10-10")
17
+ xtr.extractor("agency").call(doc).should == Nothing
18
+ end
19
+
20
+ it "returns Nothing if an expression points to a non-existant key" do
21
+ xtr.extractor("ref_anixe").call({}).should == Nothing
22
+ end
23
+
24
+ it "returns Nothing for a nested path, if an exception would be raised" do
25
+ xtr.extractor("a.b.c").call({}).should == Nothing
26
+ xtr.extractor("a.(1]").call({}).should == Nothing
27
+ end
28
+
29
+ it "returns Nothing if the underlying value of a key is nil" do
30
+ xtr.extractor("agency").call(doc).should == Nothing
31
+ end
32
+
33
+ end
@@ -16,13 +16,13 @@ describe Omnis::NestedHashExtractor do
16
16
  xtr.extractor("agency").call(doc).should be_nil
17
17
  end
18
18
 
19
- it "returns Nothing if an expression points to a non-existant key" do
20
- xtr.extractor("ref_anixe").call({}).should == Nothing
19
+ it "returns nil if an expression points to a non-existant key" do
20
+ xtr.extractor("ref_anixe").call({}).should == nil
21
21
  end
22
22
 
23
- it "returns Nothing for a nested path, if an exception would be raised" do
24
- xtr.extractor("a.b.c").call({}).should == Nothing
25
- xtr.extractor("a.(1]").call({}).should == Nothing
23
+ it "returns nil for a nested path, if an exception would be raised" do
24
+ xtr.extractor("a.b.c").call({}).should == nil
25
+ xtr.extractor("a.(1]").call({}).should == nil
26
26
  end
27
27
 
28
28
  it "returns nil if the underlying value of a key is nil" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omnis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.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-11-16 00:00:00.000000000 Z
12
+ date: 2012-11-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -170,6 +170,7 @@ files:
170
170
  - README.md
171
171
  - Rakefile
172
172
  - lib/omnis.rb
173
+ - lib/omnis/monadic_nested_hash_extractor.rb
173
174
  - lib/omnis/mongo_query.rb
174
175
  - lib/omnis/mongo_report.rb
175
176
  - lib/omnis/nested_hash_extractor.rb
@@ -178,6 +179,7 @@ files:
178
179
  - lib/omnis/transformer.rb
179
180
  - lib/omnis/version.rb
180
181
  - omnis.gemspec
182
+ - spec/lib/omnis/monadic_nested_hash_extractor_spec.rb
181
183
  - spec/lib/omnis/mongo_query_spec.rb
182
184
  - spec/lib/omnis/nested_hash_extractor_spec.rb
183
185
  - spec/lib/omnis/operators_spec.rb
@@ -197,12 +199,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
197
199
  - - ! '>='
198
200
  - !ruby/object:Gem::Version
199
201
  version: '0'
202
+ segments:
203
+ - 0
204
+ hash: -4082639984471618075
200
205
  required_rubygems_version: !ruby/object:Gem::Requirement
201
206
  none: false
202
207
  requirements:
203
208
  - - ! '>='
204
209
  - !ruby/object:Gem::Version
205
210
  version: '0'
211
+ segments:
212
+ - 0
213
+ hash: -4082639984471618075
206
214
  requirements: []
207
215
  rubyforge_project:
208
216
  rubygems_version: 1.8.23
@@ -210,6 +218,7 @@ signing_key:
210
218
  specification_version: 3
211
219
  summary: see above
212
220
  test_files:
221
+ - spec/lib/omnis/monadic_nested_hash_extractor_spec.rb
213
222
  - spec/lib/omnis/mongo_query_spec.rb
214
223
  - spec/lib/omnis/nested_hash_extractor_spec.rb
215
224
  - spec/lib/omnis/operators_spec.rb