structurematch 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/structurematch.rb +30 -10
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 99e88855ccc4edb6ae03e0f7b6679d1cf40e253b
4
- data.tar.gz: d1cb943a7475bc63a326f6d1d7d4402bffbb0185
3
+ metadata.gz: 2cfd5cfc6816e1ea70a82090af67a80c0b6fbbbf
4
+ data.tar.gz: 8fd9c2f1918f7a6620b07deb8042b1a51b3fb5cf
5
5
  SHA512:
6
- metadata.gz: 767925436ade0b040cdf96559a95db0e9b8eea1dda4077d7ebd8f9c8d12c4fc95cf2eeee9fd67a1b2b9408a1680bdb21ab95e5a30c7f106058d87aebd0fa63c2
7
- data.tar.gz: 4d67cb27f30897e3997b8ed8658f7f38b3722301b04aaf601d6bea6fe6e910013c144741a88905ee26a7aa441b10829f3be4e78280c1ca934e824f631f5c3f4d
6
+ metadata.gz: 10374c45fe240c076abcb076a045b1001e8ec45241890106ed9a9f5b49ccf3d805e346195135452f8d2799e8482598e910a8f4b33e553c7282d091eb31e6a2e7
7
+ data.tar.gz: 5f9ad1ebf6e544c613572c34e14b890bf7f478f401732d69d551d9ec0ab7b351aca7ffa1057bb4a7687c7d5c44b73ebeca8e82218a7e5e7d9954ac44fc5a75ce
@@ -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(@match)}
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(!@match.test(v)[0]),v]
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(@match > v),v]
86
- when "<" then [_t(@match < v),v]
87
- when ">=" then [_t(@match >= v),v]
88
- when "<=" then [_t(@match <= v),v]
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] = v
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.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-25 00:00:00.000000000 Z
11
+ date: 2014-08-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: A library for ferforming structural and semantic matching on JSON
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: []