hpath 0.0.4 → 0.0.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e1f0a047d687a7f9cb64a837e1d21f828bdd0669
4
- data.tar.gz: 28436f6c1f1ac651a86538ac62f18dc26a5a6868
3
+ metadata.gz: ae758397369c5c2d912cd7a10b7377683709574d
4
+ data.tar.gz: e55ba308d80557c51374868351b2a74c65abc61c
5
5
  SHA512:
6
- metadata.gz: d4c027c8649247847b822779fe2e7eea12a10d6261deb1a919ffe40724f3c62271c7dea7c40023558142224ca0db4e1f5808c32e9b3c8f567f9fd6ab6702eb6d
7
- data.tar.gz: f8a946b78d622f3a5b509177a8f040acb0043718fd88705552668c65272878ec61c1560cc6359466575d265854d5cb5548ab680f6d834ea7cd345d9c0b214e57
6
+ metadata.gz: 4fd4f632561c47eb0518b37b28ee9b1d4fcac108ba9f2758378128c7921f1f5ddf957fa9d33651ab2f50afb4c36c996b2df01b21bfa61cbcb074a102b3285ca5
7
+ data.tar.gz: 6dd7f7123466bebc8dcbdbdb96b6272f6ca29568f565bf4a5a1aeddca53701fb6e84dbab3e073552b5dcbe77487cf799b6e8ea81826fb942f319c543d8505601
@@ -93,15 +93,17 @@ module Hpath
93
93
  if identifier.to_s == "*"
94
94
  object
95
95
  else
96
- object.map do |element|
96
+ mapped_object = object.map do |element|
97
97
  if element.is_a?(Hash)
98
98
  element[identifier.to_s] || element[identifier.to_sym]
99
99
  elsif element.respond_to?(identifier)
100
100
  element.send(identifier)
101
101
  else
102
- raise "Cannot apply identifier to collection object!"
102
+ nil
103
103
  end
104
- end
104
+ end.compact.flatten(1)
105
+
106
+ mapped_object unless mapped_object.empty?
105
107
  end
106
108
  elsif object.is_a?(Hash)
107
109
  if identifier.to_s == "*"
@@ -35,7 +35,7 @@ class Hpath::Parser
35
35
  rule(:space?) { space.maybe }
36
36
 
37
37
  rule(:key_value_filter) {
38
- space? >> match['a-zA-Z'].repeat(1).maybe.as(:key) >> (str("<") | str(">") | str("=").repeat(1,3)).as(:operator) >> match['a-zA-Z0-9'].repeat(1).as(:value) >> space?
38
+ space? >> match['0-9a-zA-Z@_'].repeat(1).maybe.as(:key) >> (str("<") | str(">") | str("=").repeat(1,3)).as(:operator) >> match['a-zA-Z0-9'].repeat(1).as(:value) >> space?
39
39
  }
40
40
 
41
41
  rule(:or_filter) {
@@ -1,3 +1,3 @@
1
1
  module Hpath
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -1,3 +1,5 @@
1
+ require "some_more_complex_hpath_tests"
2
+
1
3
  describe Hpath do
2
4
  describe "#get" do
3
5
  describe "returns the corresponding value from the given object" do
@@ -0,0 +1,110 @@
1
+ describe Hpath do
2
+ describe "#get" do
3
+ let(:object) do
4
+ {
5
+ hello: "world",
6
+ empty_array: [],
7
+ empty_hash: [],
8
+ "string_key" => "foobar",
9
+ some: {
10
+ really: [
11
+ {
12
+ nested: [
13
+ {
14
+ structure: "hello"
15
+ },
16
+ {
17
+ structure: "world"
18
+ }
19
+ ]
20
+ }
21
+ ]
22
+ },
23
+ foo: [
24
+ {
25
+ bar: 1
26
+ },
27
+ {
28
+ bar: 2
29
+ }
30
+ ],
31
+ arr_of_arrs: [
32
+ [ [1,2] , [3,4,5] ],
33
+ [ 6,7 ],
34
+ 8
35
+ ],
36
+ fields: [
37
+ {"@tag"=>"020",
38
+ "@ind1"=>"a",
39
+ "@ind2"=>"1",
40
+ "subfield"=>
41
+ [{"@code"=>"a", "$"=>"2468492-2"},
42
+ {"@code"=>"b", "$"=>"DNB"}]},
43
+ {"@tag"=>"025",
44
+ "@ind1"=>"a",
45
+ "@ind2"=>"1",
46
+ "subfield"=>{"@code"=>"a", "$"=>"991563239"}},
47
+ {"@tag"=>"025",
48
+ "@ind1"=>"z",
49
+ "@ind2"=>"1",
50
+ "subfield"=>{"@code"=>"a", "$"=>"2468492-2"}},
51
+ {"@tag"=>"025",
52
+ "@ind1"=>"o",
53
+ "@ind2"=>"1",
54
+ "subfield"=>{"@code"=>"a", "$"=>"645481530"}},
55
+ {"@tag"=>"026",
56
+ "@ind1"=>"-",
57
+ "@ind2"=>"1",
58
+ "subfield"=>{"@code"=>"a", "$"=>"ZDB2468492-2"}},
59
+ ]
60
+ }
61
+ end
62
+
63
+ it "returns elements which match the given path" do
64
+ expect(described_class.get(object, "/hello")).to eq("world")
65
+ expect(described_class.get(object, "/empty_array")).to eq([])
66
+ expect(described_class.get(object, "/empty_hash")).to eq([])
67
+ expect(described_class.get(object, "/foo")).to eq([{:bar=>1}, {:bar=>2}])
68
+ expect(described_class.get(object, "/foo[0]")).to eq({:bar=>1})
69
+ expect(described_class.get(object, "/foo[1]")).to eq({:bar=>2})
70
+ expect(described_class.get(object, "/foo[]/bar")).to eq([1,2])
71
+ expect(described_class.get(object, "/arr_of_arrs[0]/[1]/[2]")).to eq(5)
72
+ expect(described_class.get(object, "/some/really[]/nested[]/structure")).to eq(["hello", "world"])
73
+ expect(described_class.get(object, "/arr_of_arrs[0]")).to eq([[1, 2], [3, 4, 5]])
74
+ end
75
+
76
+ it "returns nil if the path is invalid" do
77
+ expect(described_class.get(object, "/hello_world")).to eq(nil)
78
+ expect(described_class.get(object, "/foo[3]")).to eq(nil)
79
+ expect(described_class.get(object, "/foo[]/bar/muff")).to eq(nil)
80
+ end
81
+
82
+ it "allows searching for hashes by specifying key=value" do
83
+ expect(described_class.get(object, "/fields/*[@tag=025|@tag=026]")).to eq(
84
+ [{"@tag"=>"025",
85
+ "@ind1"=>"a",
86
+ "@ind2"=>"1",
87
+ "subfield"=>{"@code"=>"a", "$"=>"991563239"}},
88
+ {"@tag"=>"025",
89
+ "@ind1"=>"z",
90
+ "@ind2"=>"1",
91
+ "subfield"=>{"@code"=>"a", "$"=>"2468492-2"}},
92
+ {"@tag"=>"025",
93
+ "@ind1"=>"o",
94
+ "@ind2"=>"1",
95
+ "subfield"=>{"@code"=>"a", "$"=>"645481530"}},
96
+ {"@tag"=>"026",
97
+ "@ind1"=>"-",
98
+ "@ind2"=>"1",
99
+ "subfield"=>{"@code"=>"a", "$"=>"ZDB2468492-2"}}]
100
+ )
101
+ end
102
+ end
103
+
104
+ describe "#set" do
105
+ it "sets the objects value according to path and value" do
106
+ described_class.set(object = {}, "/foo[]/bar", [1,2,3])
107
+ expect(object).to eq({:foo=>[{:bar=>[1, 2, 3]}]})
108
+ end
109
+ end
110
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hpath
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Sievers
@@ -92,6 +92,7 @@ files:
92
92
  - spec/hpath/filter_spec.rb
93
93
  - spec/hpath/parser_spec.rb
94
94
  - spec/hpath_spec.rb
95
+ - spec/some_more_complex_hpath_tests.rb
95
96
  - spec/spec_helper.rb
96
97
  homepage: https://github.com/hpath/hpath-ruby
97
98
  licenses:
@@ -121,4 +122,5 @@ test_files:
121
122
  - spec/hpath/filter_spec.rb
122
123
  - spec/hpath/parser_spec.rb
123
124
  - spec/hpath_spec.rb
125
+ - spec/some_more_complex_hpath_tests.rb
124
126
  - spec/spec_helper.rb