mc-settings 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -69,8 +69,8 @@ RAILS_ROOT/config/settings/local folder):
69
69
  The following is the sequence of loading explained:
70
70
 
71
71
  * Read all files in :files list in order, and load their settings.
72
- * Last file loaded overrides previous value (if already exist).
73
- * Load all #{path}/local/*.yml files to Settings, overriding common keys. Files are loaded in order sorted by name.
72
+ * Last file loaded overrides recursively previous value (if already exist).
73
+ * Load all #{path}/local/*.yml files to Settings, overriding recursively common keys. Files are loaded in order sorted by name.
74
74
 
75
75
  == Copyright
76
76
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -1,5 +1,21 @@
1
1
  require 'singleton'
2
2
  require 'yaml'
3
+
4
+ class Hash
5
+ def recursive_merge!(other)
6
+ other.keys.each do |k|
7
+ if self[k].is_a?(Array) && other[k].is_a?(Array)
8
+ self[k] += other[k]
9
+ elsif self[k].is_a?(Hash) && other[k].is_a?(Hash)
10
+ self[k].recursive_merge!(other[k])
11
+ else
12
+ self[k] = other[k]
13
+ end
14
+ end
15
+ self
16
+ end
17
+ end
18
+
3
19
  class Setting
4
20
  class NotFound < RuntimeError; end
5
21
  class FileError < RuntimeError; end
@@ -145,7 +161,7 @@ class Setting
145
161
 
146
162
  files.flatten.each do |file|
147
163
  begin
148
- @available_settings.merge!(YAML::load(File.open(file)) || {}) if File.exists?(file)
164
+ @available_settings.recursive_merge!(YAML::load(File.open(file)) || {}) if File.exists?(file)
149
165
  rescue Exception => e
150
166
  raise FileError.new("Error parsing file #{file}, with: #{e.message}")
151
167
  end
@@ -154,6 +170,4 @@ class Setting
154
170
  @loaded = true
155
171
  @available_settings
156
172
  end
157
-
158
-
159
173
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mc-settings}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Edwin Cruz", "Colin Shield"]
12
- s.date = %q{2010-12-28}
12
+ s.date = %q{2011-01-14}
13
13
  s.description = %q{implement custom keys indenendently of environment}
14
14
  s.email = %q{rubydev@modcloth.com}
15
15
  s.extra_rdoc_files = [
@@ -10,7 +10,7 @@ describe Setting do
10
10
  :path => "config/settings",
11
11
  :local => true)
12
12
  end
13
-
13
+
14
14
  it 'should return test specific values' do
15
15
  Setting.available_settings['one'].should == "test"
16
16
  Setting.one.should == "test"
@@ -22,14 +22,13 @@ describe Setting do
22
22
  end
23
23
 
24
24
  it "handles multiple values" do
25
- Setting['six'].should == {"default"=>"default value", "extra"=>"extra"}
25
+ Setting['six'].should == {"default"=>"default value", "extra"=>"recursively overriden", "deep_level"=>{"value"=>"even deeper level"}}
26
26
  Setting.available_settings['six']['default'].should == "default value"
27
- Setting.available_settings['six']['extra'].should == "extra"
28
27
  Setting.seven.should == "seven from custom"
29
28
  end
30
29
 
31
30
  it "should support symbols as keys" do
32
- Setting[:six].should == {"default"=>"default value", "extra"=>"extra"}
31
+ Setting[:six].should == {"default"=>"default value", "extra"=>"recursively overriden"}
33
32
  end
34
33
 
35
34
  it "handles default key" do
@@ -48,31 +47,60 @@ describe Setting do
48
47
  it "should returns false correctly" do
49
48
  Setting.flag_false.should be(false)
50
49
  end
50
+
51
+ it "should merge keys recursivelly" do
52
+ Setting.six(:extra).should == "recursively overriden"
53
+ Setting.six(:deep_level)[:value] = "even deeper level"
54
+ end
55
+
56
+ it "should create keys if it does not exist" do
57
+ Setting.test_specific.should == "exist"
58
+ end
59
+ end
60
+
61
+ context "When running with threads" do
62
+ it "should keep its values" do
63
+ 3.times do |time|
64
+ Thread.new {
65
+ Proc.new{
66
+ Setting.available_settings.shoud_not be_empty
67
+ }
68
+ }
69
+ end
70
+ end
71
+ end
72
+
73
+ context "When running inside a proc" do
74
+ it "should keep its values" do
75
+ Proc.new{
76
+ Setting.available_settings.shoud_not be_empty
77
+ }
78
+ end
51
79
  end
52
80
 
53
81
  context "Test from file" do
54
82
  before :each do
55
83
  Setting.reload(
56
- :files => ['sample.yml'],
57
- :path => File.join(File.dirname(__FILE__)) + '/fixtures'
84
+ :files => ['sample.yml'],
85
+ :path => File.join(File.dirname(__FILE__)) + '/fixtures'
58
86
  )
59
87
  end
60
88
 
61
89
  it 'should support [] syntax' do
62
90
  Setting['tax']['default'].should == 0.0
63
- Setting['tax'].should == { 'default' => 0.0, 'california' => 7.5 }
91
+ Setting['tax'].should == {'default' => 0.0, 'california' => 7.5}
64
92
  end
65
93
 
66
94
  it 'should support method invocation syntax' do
67
95
  Setting.tax.should == 0.0
68
96
 
69
- Setting.tax(:default).should == Setting.tax
70
- Setting.tax('default').should == Setting.tax
71
- Setting.tax(:california).should == 7.5
97
+ Setting.tax(:default).should == Setting.tax
98
+ Setting.tax('default').should == Setting.tax
99
+ Setting.tax(:california).should == 7.5
72
100
 
73
- Setting.states.should == ['CA', 'WA', 'NY']
74
- Setting.states(:default).should == Setting.states
75
- Setting.states(:ship_to).should == ['CA', 'NY']
101
+ Setting.states.should == ['CA', 'WA', 'NY']
102
+ Setting.states(:default).should == Setting.states
103
+ Setting.states(:ship_to).should == ['CA', 'NY']
76
104
  end
77
105
 
78
106
  it 'should correctly process Boolean values' do
@@ -9,6 +9,8 @@ def stub_setting_files
9
9
  six:
10
10
  default: "default value"
11
11
  extra: "extra"
12
+ deep_level:
13
+ value: "even deeper level"
12
14
  seven:
13
15
  default: "seven"
14
16
  empty:
@@ -25,6 +27,9 @@ CONTENT
25
27
  three: 5
26
28
  four: "6"
27
29
  five: "test string"
30
+ six:
31
+ extra: "recursively overriden"
32
+ test_specific: "exist"
28
33
  CONTENT
29
34
 
30
35
  empty = <<-CONTENT
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mc-settings
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Edwin Cruz
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-12-28 00:00:00 -08:00
19
+ date: 2011-01-14 00:00:00 -08:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency