tarot 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/generators/templates/initializer.rb +2 -2
- data/lib/tarot.rb +47 -4
- data/lib/tarot/version.rb +1 -1
- data/spec/data/recursive.yml +4 -0
- data/spec/tarot_spec.rb +36 -17
- metadata +6 -4
data/lib/tarot.rb
CHANGED
@@ -3,10 +3,16 @@ require "active_support/core_ext"
|
|
3
3
|
|
4
4
|
module Tarot
|
5
5
|
class Config
|
6
|
-
attr_accessor :
|
7
|
-
def initialize(
|
8
|
-
@
|
9
|
-
@
|
6
|
+
attr_accessor :config_files, :yaml, :env
|
7
|
+
def initialize(files, env)
|
8
|
+
@config_files = files
|
9
|
+
@config_files = [@config_files] if @config_files.is_a? String
|
10
|
+
@yaml = {}
|
11
|
+
@config_files.each do |file|
|
12
|
+
yaml = YAML::load(File.open(file).read.untaint).stringify_keys!
|
13
|
+
recursive_merge @yaml, yaml
|
14
|
+
end
|
15
|
+
add_mm @yaml
|
10
16
|
@config_cache = {}
|
11
17
|
@env = env
|
12
18
|
end
|
@@ -15,5 +21,42 @@ module Tarot
|
|
15
21
|
@config_cache[env] ||= {}
|
16
22
|
@config_cache[env][key] ||= key.split('.').inject(@yaml[env || @env]) {|e, part| e.try(:[], part) } || default
|
17
23
|
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def recursive_merge(left, right)
|
28
|
+
left.merge!(right) do |key, oldval, newval|
|
29
|
+
oldval.class == left.class ? recursive_merge(oldval, newval) : newval
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def add_mm(hsh)
|
34
|
+
hsh.instance_eval do
|
35
|
+
def method_missing(method, *args)
|
36
|
+
self[method.to_s] || args.first
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
hsh.each do |key, val|
|
41
|
+
if val.class == hsh.class
|
42
|
+
add_mm val
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def method_missing(method, *args)
|
48
|
+
@yaml[args[1] || @env].send method, *args
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
module MethodMissingForHash
|
53
|
+
def self.included(obj)
|
54
|
+
obj.each do |key, val|
|
55
|
+
if obj.class == val.class
|
56
|
+
val.extend MethodMissingForHash
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
18
61
|
end
|
19
62
|
end
|
data/lib/tarot/version.rb
CHANGED
data/spec/tarot_spec.rb
CHANGED
@@ -1,27 +1,46 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Tarot do
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
context "Given a single file" do
|
5
|
+
before :all do
|
6
|
+
@tarot = Tarot::Config.new("spec/data/test.yml", "development")
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
it "should load a YAML file from a string" do
|
10
|
+
@tarot.should_not == nil
|
11
|
+
@tarot.yaml.should be_a(Hash)
|
12
|
+
@tarot.config_files.should == ["spec/data/test.yml"]
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should accept a default value for a key" do
|
16
|
+
@tarot.get("bad key").should == nil
|
17
|
+
@tarot.get("bad key", "with default").should == "with default"
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should walk a nested tree" do
|
21
|
+
@tarot.get( "nested.tree.with.value" ).should == 42
|
22
|
+
@tarot.get( "nested.tree.with.bad.key" ).should == nil
|
23
|
+
end
|
12
24
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
25
|
+
it "should accept an override environment" do
|
26
|
+
@tarot.get("fizz").should == "buzz"
|
27
|
+
@tarot.get("fizz", nil, "production").should == "bang"
|
28
|
+
end
|
17
29
|
|
18
|
-
|
19
|
-
|
20
|
-
|
30
|
+
it "should accept a method_missing chain" do
|
31
|
+
@tarot.nested.tree.with.value.should == 42
|
32
|
+
@tarot.nested.tree.with.bad_value(1234).should == 1234
|
33
|
+
@tarot.fizz.should == "buzz"
|
34
|
+
@tarot.fizz(nil, "production").should == "bang"
|
35
|
+
end
|
21
36
|
end
|
22
37
|
|
23
|
-
|
24
|
-
|
25
|
-
|
38
|
+
context "Given multiple config files" do
|
39
|
+
it "should produce a recursively merged hash when given multiple files to load" do
|
40
|
+
@tarot = Tarot::Config.new(["spec/data/test.yml", "spec/data/recursive.yml"], "development")
|
41
|
+
@tarot.get("foo").should == "fizzle"
|
42
|
+
@tarot.get("mah").should == "rizzle"
|
43
|
+
@tarot.get("fizz").should == "buzz"
|
44
|
+
end
|
26
45
|
end
|
27
46
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tarot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 2
|
10
|
+
version: 0.1.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Chris Heald
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-06-
|
18
|
+
date: 2011-06-29 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -53,6 +53,7 @@ files:
|
|
53
53
|
- generators/templates/initializer.rb
|
54
54
|
- lib/tarot.rb
|
55
55
|
- lib/tarot/version.rb
|
56
|
+
- spec/data/recursive.yml
|
56
57
|
- spec/data/test.yml
|
57
58
|
- spec/spec_helper.rb
|
58
59
|
- spec/tarot_spec.rb
|
@@ -92,6 +93,7 @@ signing_key:
|
|
92
93
|
specification_version: 3
|
93
94
|
summary: Tarot is a small, concise configuration library for Rails apps.
|
94
95
|
test_files:
|
96
|
+
- spec/data/recursive.yml
|
95
97
|
- spec/data/test.yml
|
96
98
|
- spec/spec_helper.rb
|
97
99
|
- spec/tarot_spec.rb
|