hpath 0.0.2 → 0.0.3
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 +4 -4
- data/lib/hpath.rb +37 -4
- data/lib/hpath/parser.rb +5 -3
- data/lib/hpath/version.rb +1 -1
- data/spec/hpath_spec.rb +26 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a89f4dea22fe5b719e3f2c4402806bc07eb0676
|
4
|
+
data.tar.gz: 8a49750d03f96bfaf46c1a50181778d99eecea08
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ffed5bced9a44c68d3d7d85b54580bec011178371519b4827daae79236d9230decf7ffcd6eb58eba2a8596be83490434ec1559032fe8a4308534ff1840c7c1f8
|
7
|
+
data.tar.gz: a777601d83a4633630ff28ce802577e25fe702a8ff767aa30cc20b922d51814ec986c39d1bbd8690ebd68429ee1a0d6361e24656a3f08d71dc84547952985741
|
data/lib/hpath.rb
CHANGED
@@ -8,6 +8,11 @@ module Hpath
|
|
8
8
|
_get(object, hpath[:path])
|
9
9
|
end
|
10
10
|
|
11
|
+
def self.set(object, hpath_string, value)
|
12
|
+
hpath = Hpath::Parser.parse(hpath_string)
|
13
|
+
_set(object, hpath[:path], value)
|
14
|
+
end
|
15
|
+
|
11
16
|
#
|
12
17
|
private
|
13
18
|
#
|
@@ -39,18 +44,46 @@ module Hpath
|
|
39
44
|
self._get(object, paths, _object)
|
40
45
|
end
|
41
46
|
|
47
|
+
def self._set(object, paths, value)
|
48
|
+
if paths.empty?
|
49
|
+
if object.is_a?(Array)
|
50
|
+
object.push(value)
|
51
|
+
elsif object.is_a?(Hash)
|
52
|
+
object.merge!(value)
|
53
|
+
end
|
54
|
+
|
55
|
+
return
|
56
|
+
else
|
57
|
+
path = paths.shift
|
58
|
+
end
|
59
|
+
|
60
|
+
if (_object = self._get(object, [path])).nil?
|
61
|
+
if object.is_a?(Array)
|
62
|
+
if path[:type] == Array
|
63
|
+
object.push(_object = [])
|
64
|
+
elsif path[:type] == Hash
|
65
|
+
object.push({ path[:identifier].to_sym => (_object = {}) })
|
66
|
+
end
|
67
|
+
elsif object.is_a?(Hash)
|
68
|
+
object[path[:identifier].to_sym] = (_object = path[:type].new)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
self._set(_object, paths, value)
|
73
|
+
end
|
74
|
+
|
42
75
|
def self._apply_filters(object, filter)
|
43
76
|
if object.is_a?(Array)
|
44
77
|
object.select do |element|
|
45
78
|
filter.applies?(element)
|
46
79
|
end
|
47
80
|
else
|
48
|
-
#
|
81
|
+
# TODO
|
49
82
|
end
|
50
83
|
end
|
51
84
|
|
52
85
|
def self._resolve_identifier(object, identifier)
|
53
|
-
if object.is_a?(Array)
|
86
|
+
if object.is_a?(Array) && !object.empty?
|
54
87
|
if identifier.to_s == "*"
|
55
88
|
object
|
56
89
|
else
|
@@ -71,14 +104,14 @@ module Hpath
|
|
71
104
|
object[identifier.to_s] || object[identifier.to_sym]
|
72
105
|
end
|
73
106
|
else
|
74
|
-
#
|
107
|
+
# TODO
|
75
108
|
end
|
76
109
|
end
|
77
110
|
|
78
111
|
def self._resolve_indices(object, indices)
|
79
112
|
if indices.length == 1
|
80
113
|
object[indices.first]
|
81
|
-
|
114
|
+
elsif indices.length > 1
|
82
115
|
indices.map { |index| object[index] }
|
83
116
|
end
|
84
117
|
end
|
data/lib/hpath/parser.rb
CHANGED
@@ -21,6 +21,7 @@ class Hpath::Parser
|
|
21
21
|
element[:indices] = [element[:indices]] if !element[:indices].nil? && !element[:indices].is_a?(Array)
|
22
22
|
element[:keys] ||= nil
|
23
23
|
element[:keys] = [element[:keys]] if !element[:keys].nil? && !element[:keys].is_a?(Array)
|
24
|
+
element[:type] = element[:type] == "[]" || element[:indices].is_a?(Array) ? Array : Hash
|
24
25
|
element
|
25
26
|
end
|
26
27
|
|
@@ -68,7 +69,7 @@ class Hpath::Parser
|
|
68
69
|
}
|
69
70
|
|
70
71
|
rule(:node) {
|
71
|
-
str("/") >> (identifier.as(:identifier) | (str("::") >> identifier.as(:axis))).maybe >> (str("[") >> space? >> (indices.as(:indices) | filter | keys.as(:keys)) >> space? >> str("]")).maybe
|
72
|
+
str("/") >> (identifier.as(:identifier) | (str("::") >> identifier.as(:axis))).maybe >> (str("[]").as(:type) | (str("[") >> space? >> (indices.as(:indices) | filter | keys.as(:keys)) >> space? >> str("]"))).maybe
|
72
73
|
}
|
73
74
|
|
74
75
|
rule(:path) {
|
@@ -87,13 +88,14 @@ class Hpath::Parser
|
|
87
88
|
def transformation
|
88
89
|
@transformation ||=
|
89
90
|
Class.new(Parslet::Transform) do
|
90
|
-
rule(axis: simple(:axis), identifier: simple(:identifier), filter: subtree(:filter), indices: subtree(:indices), keys: subtree(:keys)) {
|
91
|
+
rule(axis: simple(:axis), identifier: simple(:identifier), filter: subtree(:filter), indices: subtree(:indices), keys: subtree(:keys), type: simple(:type)) {
|
91
92
|
{
|
92
93
|
axis: axis.nil? ? nil : axis.to_s,
|
93
94
|
identifier: identifier.nil? ? nil : identifier.to_s,
|
94
95
|
filter: filter,
|
95
96
|
indices: indices.nil? ? nil : indices.map { |element| Integer(element[:index]) },
|
96
|
-
keys: keys.nil? ? nil : keys.map { |element| element[:key].to_s.to_sym }
|
97
|
+
keys: keys.nil? ? nil : keys.map { |element| element[:key].to_s.to_sym },
|
98
|
+
type: type
|
97
99
|
}
|
98
100
|
}
|
99
101
|
|
data/lib/hpath/version.rb
CHANGED
data/spec/hpath_spec.rb
CHANGED
@@ -66,5 +66,31 @@ describe Hpath do
|
|
66
66
|
expect(hpath_result).to eq([{ foo: { bar: "foobar" } }])
|
67
67
|
end
|
68
68
|
end
|
69
|
+
|
70
|
+
describe "returns nil for non-matching hpath expressions" do
|
71
|
+
it "does not process \"/key1\" for an empty array" do
|
72
|
+
hpath_result = Hpath.get([], "/foo")
|
73
|
+
expect(hpath_result).to eq(nil)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "#set" do
|
79
|
+
describe "sets the object identified by its hpath to the given value" do
|
80
|
+
it "processes \"/key1/key2\" for a hash" do
|
81
|
+
Hpath.set(hash = {}, "/foo/bar", { muff: "foobar"})
|
82
|
+
expect(hash).to eq({foo: { bar: { muff: "foobar"} }})
|
83
|
+
end
|
84
|
+
|
85
|
+
it "processes \"/[]/key2\" for a array" do
|
86
|
+
Hpath.set(array = [], "/[]/bar", { foo: "bar"})
|
87
|
+
expect(array).to eq([{ bar: {foo: "bar"} }])
|
88
|
+
end
|
89
|
+
|
90
|
+
it "processes \"/key1[]/key2\" for a array" do
|
91
|
+
Hpath.set(hash = {}, "/key1[]/bar", { foo: "bar"})
|
92
|
+
expect(hash).to eq({key1: [{bar: {foo: "bar"}}]})
|
93
|
+
end
|
94
|
+
end
|
69
95
|
end
|
70
96
|
end
|