chozo 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Guardfile +4 -1
- data/chozo.gemspec +1 -0
- data/lib/chozo.rb +2 -0
- data/lib/chozo/config.rb +1 -1
- data/lib/chozo/core_ext.rb +3 -0
- data/lib/chozo/core_ext/hash.rb +88 -0
- data/lib/chozo/version.rb +1 -1
- data/spec/unit/chozo/core_ext/hash_spec.rb +61 -0
- metadata +23 -3
data/Guardfile
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
notification :off
|
2
|
+
interactor :coolline
|
3
|
+
|
1
4
|
guard 'spork' do
|
2
5
|
watch('Gemfile')
|
3
6
|
watch('spec/spec_helper.rb') { :rspec }
|
@@ -10,7 +13,7 @@ guard 'yard', stdout: '/dev/null', stderr: '/dev/null' do
|
|
10
13
|
watch(%r{ext/.+\.c})
|
11
14
|
end
|
12
15
|
|
13
|
-
guard 'rspec', version: 2, cli: "--color --drb --format Fuubar", all_on_start: false, all_after_pass: false
|
16
|
+
guard 'rspec', version: 2, cli: "--color --drb --format Fuubar", all_on_start: false, all_after_pass: false do
|
14
17
|
watch(%r{^spec/unit/.+_spec\.rb$})
|
15
18
|
watch(%r{^spec/acceptance/.+_spec\.rb$})
|
16
19
|
|
data/chozo.gemspec
CHANGED
data/lib/chozo.rb
CHANGED
data/lib/chozo/config.rb
CHANGED
@@ -0,0 +1,88 @@
|
|
1
|
+
class Hash
|
2
|
+
class << self
|
3
|
+
# Create a new Hash containing other nested Hashes from a string containing
|
4
|
+
# a dotted path. A Hash will be created and assigned to a key of another Hash
|
5
|
+
# for each entry in the dotted path.
|
6
|
+
#
|
7
|
+
# If a value is provided for the optional seed argument then the value of the
|
8
|
+
# deepest nested key will be set to the given value. If no value is provided
|
9
|
+
# the value of the key will be nil.
|
10
|
+
#
|
11
|
+
# @example creating a nested hash from a dotted path
|
12
|
+
#
|
13
|
+
# Hash.from_dotted_path("deep.nested.hash") =>
|
14
|
+
# {
|
15
|
+
# "deep" => {
|
16
|
+
# "nested" => {
|
17
|
+
# "hash" => nil
|
18
|
+
# }
|
19
|
+
# }
|
20
|
+
# }
|
21
|
+
#
|
22
|
+
#
|
23
|
+
# @example specifying a seed value
|
24
|
+
#
|
25
|
+
# Hash.from_dotted_path("deep.nested.hash", :seed_value) =>
|
26
|
+
# {
|
27
|
+
# "deep" => {
|
28
|
+
# "nested" => {
|
29
|
+
# "hash" => :seed_value
|
30
|
+
# }
|
31
|
+
# }
|
32
|
+
# }
|
33
|
+
#
|
34
|
+
# @param [String, Array] dotpath
|
35
|
+
# @param [Object] seed
|
36
|
+
# @param [Hash] target
|
37
|
+
#
|
38
|
+
# @return [Hash]
|
39
|
+
def from_dotted_path(dotpath, seed = nil, target = self.new)
|
40
|
+
case dotpath
|
41
|
+
when String
|
42
|
+
from_dotted_path(dotpath.split("."), seed)
|
43
|
+
when Array
|
44
|
+
if dotpath.empty?
|
45
|
+
return target
|
46
|
+
end
|
47
|
+
|
48
|
+
key = dotpath.pop
|
49
|
+
|
50
|
+
if target.empty?
|
51
|
+
target[key] = seed
|
52
|
+
from_dotted_path(dotpath, seed, target)
|
53
|
+
else
|
54
|
+
new_target = self.new
|
55
|
+
new_target[key] = target
|
56
|
+
from_dotted_path(dotpath, seed, new_target)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Return the value of the nested hash key from the given dotted path
|
63
|
+
#
|
64
|
+
# @example
|
65
|
+
#
|
66
|
+
# nested_hash = {
|
67
|
+
# "deep" => {
|
68
|
+
# "nested" => {
|
69
|
+
# "hash" => :seed_value
|
70
|
+
# }
|
71
|
+
# }
|
72
|
+
# }
|
73
|
+
#
|
74
|
+
# nested_hash.dig('deep.nested.hash') => :seed_value
|
75
|
+
#
|
76
|
+
# @param [String] path
|
77
|
+
#
|
78
|
+
# @return [Object, nil]
|
79
|
+
def dig(path)
|
80
|
+
parts = path.split('.', 2)
|
81
|
+
match = self[parts[0]]
|
82
|
+
if !parts[1] or match.nil?
|
83
|
+
match
|
84
|
+
else
|
85
|
+
match.dig(parts[1])
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/lib/chozo/version.rb
CHANGED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Hash do
|
4
|
+
describe "ClassMethods" do
|
5
|
+
subject { Hash }
|
6
|
+
|
7
|
+
describe "::from_dotted_path" do
|
8
|
+
it "returns a new Hash" do
|
9
|
+
subject.from_dotted_path("deep.nested.item").should be_a(Hash)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "a hash containing the nested keys" do
|
13
|
+
obj = subject.from_dotted_path("deep.nested.item")
|
14
|
+
|
15
|
+
obj.should have_key("deep")
|
16
|
+
obj["deep"].should have_key("nested")
|
17
|
+
obj["deep"]["nested"].should have_key("item")
|
18
|
+
end
|
19
|
+
|
20
|
+
it "sets a nil value for the deepest nested item" do
|
21
|
+
obj = subject.from_dotted_path("deep.nested.item")
|
22
|
+
|
23
|
+
obj["deep"]["nested"]["item"].should be_nil
|
24
|
+
end
|
25
|
+
|
26
|
+
context "when given a seed value" do
|
27
|
+
it "sets the value of the deepest nested item to the seed" do
|
28
|
+
obj = subject.from_dotted_path("deep.nested.item", "seeded_value")
|
29
|
+
|
30
|
+
obj["deep"]["nested"]["item"].should eql("seeded_value")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
subject { Hash.new }
|
37
|
+
|
38
|
+
describe "#dig" do
|
39
|
+
context "when the Hash contains the nested path" do
|
40
|
+
subject do
|
41
|
+
{
|
42
|
+
"we" => {
|
43
|
+
"found" => {
|
44
|
+
"something" => true
|
45
|
+
}
|
46
|
+
}
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
it "returns the value at the dotted path" do
|
51
|
+
subject.dig("we.found.something").should be_true
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context "when the Hash does not contain the nested path" do
|
56
|
+
it "returns a nil value" do
|
57
|
+
subject.dig("nothing.is.here").should be_nil
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chozo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,8 +9,24 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-10-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
14
30
|
- !ruby/object:Gem::Dependency
|
15
31
|
name: activemodel
|
16
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -269,6 +285,8 @@ files:
|
|
269
285
|
- lib/chozo.rb
|
270
286
|
- lib/chozo/config.rb
|
271
287
|
- lib/chozo/config/json.rb
|
288
|
+
- lib/chozo/core_ext.rb
|
289
|
+
- lib/chozo/core_ext/hash.rb
|
272
290
|
- lib/chozo/errors.rb
|
273
291
|
- lib/chozo/version.rb
|
274
292
|
- spec/shared_examples/chozo/config.rb
|
@@ -276,6 +294,7 @@ files:
|
|
276
294
|
- spec/support/helpers.rb
|
277
295
|
- spec/unit/chozo/config/json_spec.rb
|
278
296
|
- spec/unit/chozo/config_spec.rb
|
297
|
+
- spec/unit/chozo/core_ext/hash_spec.rb
|
279
298
|
homepage: https://github.com/reset/chozo
|
280
299
|
licenses: []
|
281
300
|
post_install_message:
|
@@ -296,7 +315,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
296
315
|
version: '0'
|
297
316
|
segments:
|
298
317
|
- 0
|
299
|
-
hash:
|
318
|
+
hash: -2640494448512270787
|
300
319
|
requirements: []
|
301
320
|
rubyforge_project:
|
302
321
|
rubygems_version: 1.8.23
|
@@ -309,4 +328,5 @@ test_files:
|
|
309
328
|
- spec/support/helpers.rb
|
310
329
|
- spec/unit/chozo/config/json_spec.rb
|
311
330
|
- spec/unit/chozo/config_spec.rb
|
331
|
+
- spec/unit/chozo/core_ext/hash_spec.rb
|
312
332
|
has_rdoc:
|