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 +4 -4
- data/lib/berg.rb +1 -0
- data/lib/berg/base.rb +9 -0
- data/lib/berg/key.rb +7 -5
- data/lib/berg/value.rb +3 -7
- data/lib/berg/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a6c5925f3d437e3acc5c2a94861726b5bc60b28b
|
4
|
+
data.tar.gz: 2e85cf261e78a57b4fa8195780be6829eb75d1b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa243d096f19dc7e0c80c03b26cd945917de0184e2b48f3221d91750aac82d4983cdb8bb155f3f49c73c4eb9f2fd700f094125a5fbf18cac808bfa9e6d2a2915
|
7
|
+
data.tar.gz: 6dfc5617d90aab29c2046a45646a557c2e9e261adfb6980cecaab264e49b817162617d1e0834f823f2dc4f74fe5a8ce1a750ca5e8cbdd464390ef5b55af29b31
|
data/lib/berg.rb
CHANGED
data/lib/berg/base.rb
ADDED
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,
|
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,
|
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,
|
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(
|
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
|
-
|
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(/\.|(\[
|
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
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:
|
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
|