mobj 1.7.1 → 1.7.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +10 -7
  2. data/lib/mobj.rb +25 -10
  3. metadata +2 -2
data/README.md CHANGED
@@ -20,7 +20,8 @@ Fine.
20
20
  This is really the most useful part of the entire library, when you get right down to it.
21
21
  String is given a method called "tokenize" that allows it's contents to be split up into
22
22
  a structured path of tokens. This tokenized path can then be used to do some pretty amazing
23
- things in terms of walking through arbitrarily complex trees of data.
23
+ things in terms of walking through arbitrarily complex trees of data. There's a convenience method
24
+ on String called "walk" that handles this all for you.
24
25
 
25
26
  For example, given this object:
26
27
 
@@ -28,14 +29,16 @@ For example, given this object:
28
29
  obj = {
29
30
  name: { first: "Joe", last: "Smith" },
30
31
  ids: [ 1, 3, 5, 16, 941, 13, 100, 3, 0, 104 ],
31
- auth_tokens: [ { provider: { name: "example.com", id:123 }, token: { auth: "123456", expire: "10-20-2012" } },
32
- { provider: { name: "site.com", id:265 }, token: { authentication_token: "891011", date: "10-20-2013" } }
32
+ auth_tokens: [ { provider: { name: "example.com", id:123 },
33
+ token: { auth: "123456", expire: "10-20-2012" } },
34
+ { provider: { name: "site.com", id:265 },
35
+ token: { authentication_token: "891011", date: "10-20-2013" } }
33
36
  ],
34
37
  primary_key: { path: "auth_tokens.provider" }
35
38
  }
36
39
  ```
37
40
 
38
- ... Easily traverse it's data using rules embedded into a simple string, like so:
41
+ Easily traverse it's data using rules embedded into a simple string, like so:
39
42
 
40
43
  ```ruby
41
44
  # Walk a simple object path:
@@ -47,10 +50,10 @@ obj = {
47
50
  #=> ["Joe", "Smith"]
48
51
 
49
52
  # Or indexes (and ranges) in an array:
50
- "ids[1, 3, 5 - 7, 9+]".walk(obj)
53
+ "ids[1, 3, 5..7, 9+]".walk(obj)
51
54
  #=> [3, 16, 13, 100, 3, 104]
52
55
 
53
- # Use regular expressions or method calls as selection keys:
56
+ # Use regular expressions or even method calls as selection keys:
54
57
  "auth_tokens.token./^auth/.*to_i".walk(obj)
55
58
  #=> [ 123456, 891011 ]
56
59
 
@@ -148,7 +151,7 @@ hash.unknown?
148
151
  #=> True
149
152
  ```
150
153
 
151
- Matching stuff
154
+ Matching stuff:
152
155
 
153
156
  ```ruby
154
157
  match = "Joe Bob".match(/(?<first_name>\w+) (?<first_name>)/)
@@ -261,15 +261,33 @@ module Mobj
261
261
  end
262
262
  end
263
263
 
264
-
265
-
266
264
  def find(obj, match)
267
265
  if obj.is_a?(Array)
268
266
  obj.map do |child|
269
267
  find(child, match)
270
268
  end
271
269
  elsif obj.respond_to?(:keys)
272
- obj.keys.map { |key| key if key.match(match) }.compact.map{|key| obj[key] }
270
+ found = obj.keys.each.with_object({}) { |key, fnd|
271
+ m = key.to_s.match(match)
272
+ fnd[key] = m if m
273
+ }
274
+
275
+ flat = found.values.flat_map(&:captures).empty?
276
+
277
+ found.each.with_object(flat ? [] : {}) { |(key, m), map|
278
+ if map.is_a?(Array)
279
+ map << obj[key]
280
+ else
281
+ named = m.to_hash.invert
282
+ name = if named.empty?
283
+ m.captures.empty? ? key : m.captures.sequester
284
+ else
285
+ named.find { |k, v| !k.nil? }.attempt(key).last
286
+ end
287
+ map[name] = obj[key]
288
+ end
289
+ }
290
+
273
291
  end
274
292
  end
275
293
 
@@ -301,7 +319,6 @@ module Mobj
301
319
  @path.map { |token| token.walk(obj, root) }
302
320
  when :lookup
303
321
  lookup = @path.walk(obj)
304
- puts "lookup '#{lookup}':#{obj}:#{@path}"
305
322
  if lookup.is_a?(Array)
306
323
  lookup.flatten.map { |lu| lu.tokenize.walk(root) }.flatten(1)
307
324
  else
@@ -335,9 +352,7 @@ module Mobj
335
352
  matches
336
353
  end
337
354
 
338
- def walk(obj)
339
- tokenize.walk(obj)
340
- end
355
+ def walk(obj) tokenize.walk(obj) end
341
356
 
342
357
  def tokenize
343
358
  tokens = []
@@ -361,9 +376,9 @@ module Mobj
361
376
  elsif match.up?
362
377
  tokens << Token.new(:up)
363
378
  elsif match.path?
364
- eachs = match.path.split(",")
365
- ors = match.path.split("|")
366
- ands = match.path.split("&")
379
+ eachs = match.path.split(/\s*,\s*/)
380
+ ors = match.path.split(/\s*\|\s*/)
381
+ ands = match.path.split(/\s*\&\s*/)
367
382
  if eachs.size > 1
368
383
  tokens << Token.new(:each, eachs.map { |token| token.tokenize() })
369
384
  elsif ands.size > 1
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mobj
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.1
4
+ version: 1.7.3
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-21 00:00:00.000000000 Z
12
+ date: 2012-12-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec