structurematch 0.1.1 → 0.1.2
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/structurematch.rb +30 -10
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2cfd5cfc6816e1ea70a82090af67a80c0b6fbbbf
|
4
|
+
data.tar.gz: 8fd9c2f1918f7a6620b07deb8042b1a51b3fb5cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 10374c45fe240c076abcb076a045b1001e8ec45241890106ed9a9f5b49ccf3d805e346195135452f8d2799e8482598e910a8f4b33e553c7282d091eb31e6a2e7
|
7
|
+
data.tar.gz: 5f9ad1ebf6e544c613572c34e14b890bf7f478f401732d69d551d9ec0ab7b351aca7ffa1057bb4a7687c7d5c44b73ebeca8e82218a7e5e7d9954ac44fc5a75ce
|
data/lib/structurematch.rb
CHANGED
@@ -46,7 +46,7 @@ class StructureMatch
|
|
46
46
|
case @op
|
47
47
|
when "and","or"
|
48
48
|
raise "#{op.inspect} match key must be an array of submatches" unless @match.kind_of?(Array)
|
49
|
-
@match.map!{|m|Comparator.new(
|
49
|
+
@match.map!{|m|Comparator.new(m)}
|
50
50
|
when "not"
|
51
51
|
@match = Comparator.new(@match)
|
52
52
|
when "range"
|
@@ -66,9 +66,9 @@ class StructureMatch
|
|
66
66
|
# comparisons performed by member (which returns the set intersection of the arrays)
|
67
67
|
def test(v=true)
|
68
68
|
case @op
|
69
|
-
when "and" then [_t(@match.all?{|m|m.test(v)[0]}),v]
|
70
|
-
when "or" then [_t(@match.any?{|m|m.test(v)[0]}),v]
|
71
|
-
when "not" then [_t(
|
69
|
+
when "and" then [_t(@match.all?{|m|m.test(v)[0] > 0}),v]
|
70
|
+
when "or" then [_t(@match.any?{|m|m.test(v)[0] > 0}),v]
|
71
|
+
when "not" then [_t(@match.test(v)[0] < 0),v]
|
72
72
|
when "range" then [_t(@match === v),v]
|
73
73
|
when "regex"
|
74
74
|
r = @match.match(v)
|
@@ -82,10 +82,10 @@ class StructureMatch
|
|
82
82
|
end
|
83
83
|
when "==" then [_t(@match == v),v]
|
84
84
|
when "!=" then [_t(@match != v),v]
|
85
|
-
when ">" then [_t(
|
86
|
-
when "<" then [_t(
|
87
|
-
when ">=" then [_t(
|
88
|
-
when "<=" then [_t(
|
85
|
+
when ">" then [_t(v > @match),v]
|
86
|
+
when "<" then [_t(v < @match),v]
|
87
|
+
when ">=" then [_t(v >= @match),v]
|
88
|
+
when "<=" then [_t(v <= @match),v]
|
89
89
|
else
|
90
90
|
raise "Comparator cannot handle #{@op}"
|
91
91
|
end
|
@@ -165,7 +165,7 @@ class StructureMatch
|
|
165
165
|
binds[k] = res if offset > 0
|
166
166
|
when v.kind_of?(StructureMatch)
|
167
167
|
res,offset = v.bind(val[k])
|
168
|
-
binds[k] =
|
168
|
+
binds[k] = res unless res.empty?
|
169
169
|
else
|
170
170
|
raise "StructureMatch.bind: No idea how to handle #{v.class.name}: #{v.inspect}"
|
171
171
|
end
|
@@ -174,9 +174,29 @@ class StructureMatch
|
|
174
174
|
[binds,score]
|
175
175
|
end
|
176
176
|
|
177
|
+
# As bind, but transforms the nested hash into a flat hash with nested keys
|
178
|
+
# joined by sep.
|
179
|
+
def flatbind(val, sep='.')
|
180
|
+
binds,score = bind(val)
|
181
|
+
target = Hash.new
|
182
|
+
prefix = ''
|
183
|
+
_flatbind = lambda do |vals,p|
|
184
|
+
vals.each do |k,v|
|
185
|
+
key = p.empty? ? k : "#{p}#{sep}#{k.to_s}"
|
186
|
+
if v.kind_of?(Hash)
|
187
|
+
_flatbind.call(v,key)
|
188
|
+
else
|
189
|
+
target[key] = v
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
_flatbind.call(binds,"")
|
194
|
+
[target,score]
|
195
|
+
end
|
196
|
+
|
177
197
|
# Runs bind on val and returns just the score component.
|
178
198
|
def score(val)
|
179
199
|
bind(val)[1]
|
180
200
|
end
|
181
|
-
|
201
|
+
|
182
202
|
end
|
metadata
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: structurematch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Victor Lowther
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-27 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: A library for
|
13
|
+
description: A library for performing structural and semantic matching on JSON
|
14
14
|
email: victor.lowther@gmail.com
|
15
15
|
executables: []
|
16
16
|
extensions: []
|