berg 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1db42bed4f1d7e59d44ca2eb2a154202d1ae1023
4
- data.tar.gz: 3b8a595b4e9fda38c3a447aad6bbf09a40fa1a9c
3
+ metadata.gz: a6c5925f3d437e3acc5c2a94861726b5bc60b28b
4
+ data.tar.gz: 2e85cf261e78a57b4fa8195780be6829eb75d1b3
5
5
  SHA512:
6
- metadata.gz: 0d03491c493610fd9093e7e254f5c410005adb1c431302021748aead7df817a00702f6073a886fa5594359d2fc8f8cb789380bf298059c649a835db653bdf094
7
- data.tar.gz: b96f97617248045fa7b6d52f9b2884e7f912758d0cbae4c8fc59dbd013e50cd83435da8553cea6b34a7342df1c00fcd0306351dc4b71ceba0cfdcdadd65104cf
6
+ metadata.gz: fa243d096f19dc7e0c80c03b26cd945917de0184e2b48f3221d91750aac82d4983cdb8bb155f3f49c73c4eb9f2fd700f094125a5fbf18cac808bfa9e6d2a2915
7
+ data.tar.gz: 6dfc5617d90aab29c2046a45646a557c2e9e261adfb6980cecaab264e49b817162617d1e0834f823f2dc4f74fe5a8ce1a750ca5e8cbdd464390ef5b55af29b31
data/lib/berg.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "berg/version"
2
+ require "berg/base"
2
3
  require "berg/value"
3
4
  require "berg/key"
4
5
  require "berg/container"
data/lib/berg/base.rb ADDED
@@ -0,0 +1,9 @@
1
+ module Berg
2
+ module Base
3
+ ARRAY_ID = /\[(-?\d+)\]/
4
+
5
+ def add(trace, key)
6
+ trace + (key.match(ARRAY_ID) ? key : ".#{key}")
7
+ end
8
+ end
9
+ end
data/lib/berg/key.rb CHANGED
@@ -1,29 +1,31 @@
1
1
  module Berg
2
2
  class Key
3
+ include Berg::Base
4
+
3
5
  def initialize(&block)
4
6
  @block = block
5
7
  end
6
8
 
7
9
  def self.locate(object, &block)
8
- new(&block).locate(object, "")
10
+ new(&block).locate(object)
9
11
  end
10
12
 
11
- def locate(object, current)
13
+ def locate(object, trace = "")
12
14
  case object
13
15
  when Array
14
16
  object.each_with_index do |rest, index|
15
- if (result = locate(rest, current + "[#{index}]")).found?
17
+ if (result = locate(rest, add(trace, "[#{index}]"))).found?
16
18
  return result
17
19
  end
18
20
  end
19
21
  when Hash
20
22
  object.each_pair do |key, value|
21
- if (result = locate(value, current + ".#{key}")).found?
23
+ if (result = locate(value, add(trace, key))).found?
22
24
  return result
23
25
  end
24
26
  end
25
27
  when TrueClass, FalseClass, NilClass, Fixnum, String
26
- return @block.call(object) ? Found.new(current) : NotFound.new
28
+ return @block.call(object) ? Found.new(trace) : NotFound.new
27
29
  when Found, NotFound
28
30
  return object
29
31
  else
data/lib/berg/value.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Berg
2
2
  class Value
3
- ARRAY_ID = /\[(\d+)\]/
3
+ include Berg::Base
4
4
 
5
5
  def initialize(object, query, debug = false)
6
6
  @object = object
@@ -15,7 +15,7 @@ module Berg
15
15
  def locate
16
16
  result, _ = keys.inject([@object, ""]) do |(latest, trace), key|
17
17
  found = case key
18
- when ARRAY_ID
18
+ when Base::ARRAY_ID
19
19
  latest[$1.to_i]
20
20
  else
21
21
  latest[key] || latest[key.to_sym]
@@ -37,11 +37,7 @@ module Berg
37
37
  private
38
38
 
39
39
  def keys
40
- @query.split(/\.|(\[\d+\])/).reject(&:empty?)
41
- end
42
-
43
- def add(trace, key)
44
- trace << (key.match(ARRAY_ID) ? key : ".#{key}")
40
+ @query.split(/\.|(\[-?\d+\])/).reject(&:empty?)
45
41
  end
46
42
  end
47
43
  end
data/lib/berg/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Berg
2
- VERSION = "0.1.0"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: berg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Linus Oleander
@@ -81,6 +81,7 @@ files:
81
81
  - Rakefile
82
82
  - berg.gemspec
83
83
  - lib/berg.rb
84
+ - lib/berg/base.rb
84
85
  - lib/berg/container.rb
85
86
  - lib/berg/errors.rb
86
87
  - lib/berg/key.rb