abyss 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/abyss.rb +58 -12
- data/lib/abyss/errors.rb +12 -0
- data/lib/abyss/version.rb +1 -1
- data/spec/abyss_spec.rb +46 -17
- metadata +16 -5
data/lib/abyss.rb
CHANGED
@@ -21,30 +21,76 @@ module Abyss
|
|
21
21
|
#
|
22
22
|
# Examples:
|
23
23
|
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
24
|
+
# Abyss.configure do
|
25
|
+
# three do
|
26
|
+
# levels do
|
27
|
+
# deep do
|
28
|
+
# day "Monday"
|
29
|
+
# end
|
30
|
+
# end
|
29
31
|
# end
|
30
32
|
# end
|
31
|
-
# end
|
32
|
-
# end
|
33
33
|
#
|
34
|
-
#
|
35
|
-
#
|
34
|
+
# Abyss.has?("three/levels/deep/day") #=> true
|
35
|
+
# Abyss.has?("non/existent/thing") #=> false
|
36
36
|
#
|
37
37
|
def self.has?(path)
|
38
|
+
get(path) ? true : false
|
39
|
+
end
|
40
|
+
|
41
|
+
# Gets a value at a given configuration path, slash-separated
|
42
|
+
#
|
43
|
+
# Returns: Target value or nil
|
44
|
+
#
|
45
|
+
# Examples:
|
46
|
+
#
|
47
|
+
# Abyss.configure do
|
48
|
+
# three do
|
49
|
+
# levels do
|
50
|
+
# deep do
|
51
|
+
# day "Monday"
|
52
|
+
# end
|
53
|
+
# end
|
54
|
+
# end
|
55
|
+
# end
|
56
|
+
#
|
57
|
+
# Abyss.get("three/levels/deep/day") #=> "Monday"
|
58
|
+
# Abyss.has("non/existent/thing") #=> nil
|
59
|
+
#
|
60
|
+
def self.get(path)
|
38
61
|
path.split('/').inject(Abyss.configuration) do |acc, current_path_item|
|
39
62
|
begin
|
40
63
|
target = acc.send(current_path_item)
|
41
|
-
return
|
64
|
+
return nil if target.nil?
|
42
65
|
rescue NoMethodError
|
43
|
-
return
|
66
|
+
return nil
|
44
67
|
end
|
45
68
|
target
|
46
69
|
end
|
47
|
-
|
70
|
+
end
|
71
|
+
|
72
|
+
# Gets a value at a given configuration path, slash-separated. This version
|
73
|
+
# raises an error if the value isn't defined.
|
74
|
+
#
|
75
|
+
# Returns: Target value
|
76
|
+
#
|
77
|
+
# Examples:
|
78
|
+
#
|
79
|
+
# Abyss.configure do
|
80
|
+
# three do
|
81
|
+
# levels do
|
82
|
+
# deep do
|
83
|
+
# day "Monday"
|
84
|
+
# end
|
85
|
+
# end
|
86
|
+
# end
|
87
|
+
# end
|
88
|
+
#
|
89
|
+
# Abyss.get("three/levels/deep/day") #=> "Monday"
|
90
|
+
# Abyss.has("non/existent/thing") # Raises Abyss::Errors::NotFound
|
91
|
+
#
|
92
|
+
def self.get!(path)
|
93
|
+
get(path) || raise(Abyss::Errors::NotFound.new(path))
|
48
94
|
end
|
49
95
|
|
50
96
|
end
|
data/lib/abyss/errors.rb
CHANGED
@@ -2,6 +2,18 @@ module Abyss
|
|
2
2
|
|
3
3
|
module Errors
|
4
4
|
|
5
|
+
class NotFound < RuntimeError
|
6
|
+
attr_accessor :path
|
7
|
+
|
8
|
+
def initialize(path)
|
9
|
+
self.path = path
|
10
|
+
end
|
11
|
+
|
12
|
+
def message
|
13
|
+
"Value at '#{path}' not found."
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
5
17
|
class AbstractMethod < RuntimeError
|
6
18
|
attr_accessor :name
|
7
19
|
|
data/lib/abyss/version.rb
CHANGED
data/spec/abyss_spec.rb
CHANGED
@@ -18,34 +18,63 @@ describe Abyss do
|
|
18
18
|
|
19
19
|
end
|
20
20
|
|
21
|
-
describe '
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
deep do
|
30
|
-
here "hey!"
|
31
|
-
end
|
21
|
+
describe 'access and validation mechanisms' do
|
22
|
+
|
23
|
+
before do
|
24
|
+
Abyss.configure do
|
25
|
+
three do
|
26
|
+
levels do
|
27
|
+
deep do
|
28
|
+
here "hey!"
|
32
29
|
end
|
33
30
|
end
|
34
31
|
end
|
35
32
|
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '.has?' do
|
36
|
+
|
37
|
+
it 'returns true when the value exists' do
|
38
|
+
Abyss.has?('three/levels/deep/here').should be_true
|
39
|
+
end
|
40
|
+
|
41
|
+
context "when the config doesn't exist" do
|
36
42
|
|
37
|
-
|
43
|
+
before do
|
44
|
+
Abyss.configuration = nil
|
45
|
+
Abyss.configure {}
|
46
|
+
end
|
47
|
+
|
48
|
+
specify { Abyss.has?('three/levels/deep/here').should be_false }
|
49
|
+
specify { Abyss.has?('nope').should be_false }
|
50
|
+
|
51
|
+
end
|
38
52
|
|
39
53
|
end
|
40
54
|
|
41
|
-
|
55
|
+
describe '.get' do
|
56
|
+
|
57
|
+
it 'returns the value when it exists' do
|
58
|
+
Abyss.get('three/levels/deep/here').should == 'hey!'
|
59
|
+
end
|
42
60
|
|
43
|
-
|
44
|
-
Abyss.
|
61
|
+
it "returns nil if it doesn't" do
|
62
|
+
Abyss.get('nope').should be_false
|
63
|
+
Abyss.get('no/can/do').should be_false
|
45
64
|
end
|
46
65
|
|
47
|
-
|
48
|
-
|
66
|
+
end
|
67
|
+
|
68
|
+
describe '.get!' do
|
69
|
+
|
70
|
+
it 'returns the value when it exists' do
|
71
|
+
Abyss.get('three/levels/deep/here').should == 'hey!'
|
72
|
+
end
|
73
|
+
|
74
|
+
it "raises an error when it doesn't" do
|
75
|
+
expect { Abyss.get!('nope').should be_false }.to raise_error Abyss::Errors::NotFound, /'nope' not found/
|
76
|
+
expect { Abyss.get!('no/can/do').should be_false }.to raise_error Abyss::Errors::NotFound, /'no\/can\/do' not found/
|
77
|
+
end
|
49
78
|
|
50
79
|
end
|
51
80
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: abyss
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-06-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,7 +21,12 @@ dependencies:
|
|
21
21
|
version: 3.2.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.2.0
|
25
30
|
description: Manage arbitrarily-deep configurations through a friendly DSL.
|
26
31
|
email:
|
27
32
|
- jesseltrimble@gmail.com
|
@@ -58,15 +63,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
58
63
|
- - ! '>='
|
59
64
|
- !ruby/object:Gem::Version
|
60
65
|
version: '0'
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
hash: -772610373107186531
|
61
69
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
70
|
none: false
|
63
71
|
requirements:
|
64
72
|
- - ! '>='
|
65
73
|
- !ruby/object:Gem::Version
|
66
74
|
version: '0'
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
hash: -772610373107186531
|
67
78
|
requirements: []
|
68
79
|
rubyforge_project:
|
69
|
-
rubygems_version: 1.8.
|
80
|
+
rubygems_version: 1.8.24
|
70
81
|
signing_key:
|
71
82
|
specification_version: 3
|
72
83
|
summary: Manage arbitrarily-deep configurations through a friendly DSL.
|