defaultable 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/defaultable.gemspec CHANGED
@@ -19,4 +19,5 @@ Gem::Specification.new do |gem|
19
19
 
20
20
  gem.add_development_dependency("rspec", "~> 2.7")
21
21
  gem.add_development_dependency("pry")
22
+ gem.add_development_dependency("awesome_print")
22
23
  end
@@ -3,7 +3,7 @@ require 'active_support/core_ext/hash'
3
3
  module Defaultable
4
4
  class Registry
5
5
  def initialize
6
- @table = {}
6
+ @table = HashWithIndifferentAccess.new
7
7
  end
8
8
 
9
9
  def add(key, value)
@@ -22,11 +22,5 @@ module Defaultable
22
22
  hash
23
23
  end
24
24
  end
25
-
26
- # def diff(defaults)
27
- # settings = self.as_hash
28
-
29
- # settings.diff(defaults)
30
- # end
31
25
  end
32
26
  end
@@ -43,9 +43,9 @@ module Defaultable
43
43
  value.parent = self
44
44
  end
45
45
 
46
- if self.parent && self.parent.registry_enabled
47
- self.registry.add(key, value)
48
- self.parent.registry.add(self.root_key, self.class.new(self.registry.as_hash))
46
+ if self.parent
47
+ self.registry.add(key, value)
48
+ update_parent_registries
49
49
  end
50
50
 
51
51
  @table[key] = value
@@ -89,6 +89,13 @@ module Defaultable
89
89
  self.send("#{key}=", self.class.new(hash, true, self.registry_enabled))
90
90
  end
91
91
  end
92
+
93
+ def update_parent_registries
94
+ if self.parent && self.root_key
95
+ self.parent.registry.add(self.root_key, self)
96
+ self.parent.update_parent_registries
97
+ end
98
+ end
92
99
 
93
100
  class << self
94
101
  def set_defaults(settings, env=nil)
@@ -1,3 +1,3 @@
1
1
  module Defaultable
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+ require 'awesome_print'
2
3
 
3
4
  describe Defaultable::Registry do
4
5
  before(:each) do
@@ -9,32 +10,32 @@ describe Defaultable::Registry do
9
10
  DummySetting.set_defaults Hash.new
10
11
  end
11
12
 
12
- it "should log settings to the registry" do
13
+ it "should log settings to the registry." do
13
14
  setting = DummySetting.new
14
15
  setting.blah = 'asdf'
15
16
 
16
17
  setting.registry.as_hash.should have_key('blah')
17
18
  end
18
19
 
19
- it "should not log defaults to the registry" do
20
+ it "should not log defaults to the registry." do
20
21
  DummySetting.set_defaults :foo => 'bar'
21
22
 
22
23
  setting = DummySetting.new
23
24
  setting.registry.as_hash.should_not have_key('foo')
24
25
  end
25
26
 
26
- it "should set registry values for nested attributes" do
27
+ it "should set registry values for nested attributes." do
27
28
  setting = DummySetting.new(:stuff => {:bar => 'foo'})
28
29
  setting.registry.as_hash['stuff'].should have_key('bar')
29
30
  end
30
31
 
31
- it "should not set nested attributes registry values even in nested defaults" do
32
+ it "should not set nested attributes registry values even in nested defaults." do
32
33
  DummySetting.set_defaults :values => {:foo => 'bar'}
33
34
 
34
35
  DummySetting.new.registry.as_hash.should_not have_key('values')
35
36
  end
36
37
 
37
- it "should set nested attributes registry values even in nested defaults" do
38
+ it "should set nested attributes registry values even in nested defaults." do
38
39
  DummySetting.set_defaults :values => {:foo => 'bar'}
39
40
 
40
41
  setting = DummySetting.new(:values => {:bar => 'foo'})
@@ -42,7 +43,7 @@ describe Defaultable::Registry do
42
43
  setting.values.bar?.should be_true
43
44
  end
44
45
 
45
- it "should set nested attributes registry values and retain nested defaults" do
46
+ it "should set nested attributes registry values and retain nested defaults." do
46
47
  DummySetting.set_defaults :values => {:foo => 'bar'}
47
48
 
48
49
  setting = DummySetting.new(:values => {:bar => 'foo'})
@@ -50,4 +51,14 @@ describe Defaultable::Registry do
50
51
  setting.values.bar?.should be_true
51
52
  setting.values.foo?.should be_true
52
53
  end
54
+
55
+ it "should set really nested attributes after defaults are set." do
56
+ DummySetting.set_defaults :values => { :foo => { :foo => 'fighters'} }
57
+
58
+ setting = DummySetting.new
59
+ setting.values.foo.boo = 'notta'
60
+
61
+ setting.registry.as_hash['values']['foo'].should have_key('boo')
62
+ setting.registry.as_hash['values']['foo'].should_not have_key('foo')
63
+ end
53
64
  end
@@ -10,11 +10,11 @@ describe Defaultable::Serialization do
10
10
  DummySetting.set_defaults Hash.new
11
11
  end
12
12
 
13
- it "should set a class for settings" do
13
+ it "should set a class for settings." do
14
14
  Defaultable::Serialization.settings_class.should eq(DummySetting)
15
15
  end
16
16
 
17
- it "should serialize a settings object" do
17
+ it "should serialize a settings object." do
18
18
  encoder = Defaultable::Serialization.new
19
19
  settings = DummySetting.new
20
20
 
@@ -23,7 +23,7 @@ describe Defaultable::Serialization do
23
23
  serialized.should be_a String
24
24
  end
25
25
 
26
- it "should deserialize a settings object back to a kind of Defaultable::Settings" do
26
+ it "should deserialize a settings object back to a kind of Defaultable::Settings." do
27
27
  encoder = Defaultable::Serialization.new
28
28
  settings = DummySetting.new
29
29
 
@@ -33,19 +33,19 @@ describe Defaultable::Serialization do
33
33
  encoder.load(serialized).should be_kind_of Defaultable::Settings
34
34
  end
35
35
 
36
- it "should throw an exception when the class doesn't match the settings class on load" do
36
+ it "should throw an exception when the class doesn't match the settings class on load." do
37
37
  encoder = Defaultable::Serialization.new
38
38
 
39
39
  lambda { encoder.load('adfghjsfjdhg') }.should raise_error TypeError
40
40
  end
41
41
 
42
- it "should throw an exception when the class doesn't match the settings class on dump" do
42
+ it "should throw an exception when the class doesn't match the settings class on dump." do
43
43
  encoder = Defaultable::Serialization.new
44
44
 
45
45
  lambda { encoder.dump('adfghjsfjdhg'.to_yaml) }.should raise_error TypeError
46
46
  end
47
47
 
48
- it "should load with keys still in tact" do
48
+ it "should load with keys still in tact." do
49
49
  encoder = Defaultable::Serialization.new
50
50
  settings = DummySetting.new
51
51
 
@@ -56,7 +56,7 @@ describe Defaultable::Serialization do
56
56
  unserialized.foo.should eq('bar')
57
57
  end
58
58
 
59
- it "should load defaults in after the fact" do
59
+ it "should load defaults in after the fact." do
60
60
  encoder = Defaultable::Serialization.new
61
61
  settings = DummySetting.new
62
62
 
@@ -69,7 +69,7 @@ describe Defaultable::Serialization do
69
69
  unserialized.bobby?.should be_true
70
70
  end
71
71
 
72
- it "should not serialize defaults that didn't get overwritten" do
72
+ it "should not serialize defaults that didn't get overwritten." do
73
73
  encoder = Defaultable::Serialization.new
74
74
  settings = DummySetting.new
75
75
 
@@ -82,7 +82,7 @@ describe Defaultable::Serialization do
82
82
  unserialized.bobby?.should be_false
83
83
  end
84
84
 
85
- it "should not serialize nested defaults that didn't get overwritten" do
85
+ it "should not serialize nested defaults that didn't get overwritten." do
86
86
  encoder = Defaultable::Serialization.new
87
87
  settings = DummySetting.new
88
88
 
@@ -95,7 +95,7 @@ describe Defaultable::Serialization do
95
95
  unserialized.bobby?.should be_false
96
96
  end
97
97
 
98
- it "should not serialize nested defaults that didn't get overwritten (but other values did)" do
98
+ it "should not serialize nested defaults that didn't get overwritten (but other values did)." do
99
99
  encoder = Defaultable::Serialization.new
100
100
  DummySetting.set_defaults :bobby => {:effing => 'tables'}
101
101
  settings = DummySetting.new
@@ -106,4 +106,16 @@ describe Defaultable::Serialization do
106
106
  unserialized = encoder.raw_load(serialized)
107
107
  unserialized.bobby?.should be_true
108
108
  end
109
+
110
+ it "should serialize really nested defaults that were overwritten." do
111
+ encoder = Defaultable::Serialization.new
112
+ DummySetting.set_defaults :bobby => {:effing => {:stillgoing => 'tables'}}
113
+ settings = DummySetting.new
114
+
115
+ settings.bobby.effing.stillgoing = 'ross'
116
+ serialized = encoder.dump(settings)
117
+
118
+ unserialized = encoder.raw_load(serialized)
119
+ unserialized.bobby.effing.stillgoing?.should be_true
120
+ end
109
121
  end
@@ -5,55 +5,55 @@ describe Defaultable::Settings do
5
5
  Defaultable::Settings.set_defaults({})
6
6
  end
7
7
 
8
- it "should initialize with a hash" do
8
+ it "should initialize with a hash." do
9
9
  setting = Defaultable::Settings.new({:child => 'Bobert'})
10
10
  setting.child.should eq('Bobert')
11
11
  end
12
12
 
13
- it "should be able to set nonexsistent keys" do
13
+ it "should be able to set nonexsistent keys." do
14
14
  setting = Defaultable::Settings.new
15
15
  setting.child = 'Bri Bri'
16
16
  setting.child.should eq('Bri Bri')
17
17
  end
18
18
 
19
- it "should be able to set nonexsistent keys to another setting" do
19
+ it "should be able to set nonexsistent keys to another setting." do
20
20
  setting = Defaultable::Settings.new
21
21
  setting.child = Defaultable::Settings.new
22
22
  setting.child.name = 'Mocha'
23
23
  setting.child.name.should eq('Mocha')
24
24
  end
25
25
 
26
- it "should accept nested hashes" do
26
+ it "should accept nested hashes." do
27
27
  setting = Defaultable::Settings.new({:parent => {:child => 'Rob'}})
28
28
  setting.parent.child.should eq('Rob')
29
29
  end
30
30
 
31
- it "should have a question mark method for keys" do
31
+ it "should have a question mark method for keys." do
32
32
  setting = Defaultable::Settings.new(:foo => 'bar')
33
33
  setting.foo?.should be_true
34
34
  end
35
35
 
36
- describe "Defaults" do
37
- it "should have default settings" do
36
+ describe "Defaults." do
37
+ it "should have default settings." do
38
38
  Defaultable::Settings.set_defaults File.expand_path('../', __FILE__) + '/test.yml'
39
39
  Defaultable::Settings.defaults.should be_kind_of(Hash)
40
40
  end
41
41
 
42
- it "should have a default setting for a key" do
42
+ it "should have a default setting for a key." do
43
43
  Defaultable::Settings.set_defaults File.expand_path('../', __FILE__) + '/test.yml'
44
44
 
45
45
  setting = Defaultable::Settings.new
46
46
  setting.grandparent.should be_kind_of Defaultable::Settings
47
47
  end
48
48
 
49
- it "should have another key for another setting" do
49
+ it "should have another key for another setting." do
50
50
  Defaultable::Settings.set_defaults File.expand_path('../', __FILE__) + '/test.yml'
51
51
 
52
52
  setting = Defaultable::Settings.new
53
53
  setting.grandparent.child.grandchild1.should eq('robert')
54
54
  end
55
55
 
56
- it "should set a key but still have defaults" do
56
+ it "should set a key but still have defaults." do
57
57
  Defaultable::Settings.set_defaults File.expand_path('../', __FILE__) + '/test.yml'
58
58
 
59
59
  setting = Defaultable::Settings.new({
@@ -68,7 +68,7 @@ describe Defaultable::Settings do
68
68
  setting.grandparent.child.grandchild2.should eq('brian')
69
69
  end
70
70
 
71
- it "should be able to overwrite a default" do
71
+ it "should be able to overwrite a default." do
72
72
  Defaultable::Settings.set_defaults File.expand_path('../', __FILE__) + '/test.yml'
73
73
 
74
74
  setting = Defaultable::Settings.new({
@@ -84,7 +84,7 @@ describe Defaultable::Settings do
84
84
  setting.grandparent.child.grandchild2.should eq('drpepper')
85
85
  end
86
86
 
87
- it "should be able to set a key in the middle of defaults" do
87
+ it "should be able to set a key in the middle of defaults." do
88
88
  Defaultable::Settings.set_defaults File.expand_path('../', __FILE__) + '/test.yml'
89
89
 
90
90
  setting = Defaultable::Settings.new({
@@ -100,14 +100,14 @@ describe Defaultable::Settings do
100
100
  setting.grandparent.someotherkey.should eq('saweet')
101
101
  end
102
102
 
103
- it "should be able to set a hash for defaults" do
103
+ it "should be able to set a hash for defaults." do
104
104
  Defaultable::Settings.set_defaults :child => 'sxephil'
105
105
 
106
106
  setting = Defaultable::Settings.new
107
107
  setting.child.should eq('sxephil')
108
108
  end
109
109
 
110
- it "should accept a filename with an environment" do
110
+ it "should accept a filename with an environment." do
111
111
  Defaultable::Settings.set_defaults File.expand_path('../', __FILE__) + '/env_test.yml', 'development'
112
112
 
113
113
  setting = Defaultable::Settings.new
@@ -115,8 +115,8 @@ describe Defaultable::Settings do
115
115
  end
116
116
  end
117
117
 
118
- describe "Extendable" do
119
- it "should be extendable" do
118
+ describe "Extendable." do
119
+ it "should be extendable." do
120
120
  class DummySetting < Defaultable::Settings
121
121
  set_defaults :movie => 'Iron Man'
122
122
  end
@@ -125,7 +125,7 @@ describe Defaultable::Settings do
125
125
  setting.movie.should eq('Iron Man')
126
126
  end
127
127
 
128
- it "should return settings the same class of the extension for detauls" do
128
+ it "should return settings the same class of the extension for detauls." do
129
129
  class DummySetting < Defaultable::Settings
130
130
  set_defaults :movie => 'Iron Man'
131
131
  end
@@ -134,8 +134,8 @@ describe Defaultable::Settings do
134
134
  end
135
135
  end
136
136
 
137
- describe "Hashes" do
138
- it ".as_hash should return a hash" do
137
+ describe "Hashes." do
138
+ it ".as_hash should return a hash." do
139
139
  Defaultable::Settings.set_defaults :child => 'sxephil'
140
140
  setting = Defaultable::Settings.new
141
141
 
@@ -143,7 +143,7 @@ describe Defaultable::Settings do
143
143
  setting.as_hash.should be_kind_of Hash
144
144
  end
145
145
 
146
- it ".as_hash should return a hash with the correct keys" do
146
+ it ".as_hash should return a hash with the correct keys." do
147
147
  Defaultable::Settings.set_defaults :child => 'sxephil'
148
148
  setting = Defaultable::Settings.new
149
149
 
@@ -152,24 +152,24 @@ describe Defaultable::Settings do
152
152
  end
153
153
  end
154
154
 
155
- describe "Defaults" do
155
+ describe "Defaults." do
156
156
  before(:each) do
157
157
  class DummySetting < Defaultable::Settings
158
158
  set_defaults :movie => {:name => 'Iron Man' }
159
159
  end
160
160
  end
161
161
 
162
- it "should have a key from defaults on initialization" do
162
+ it "should have a key from defaults on initialization." do
163
163
  setting = DummySetting.new
164
164
  setting.movie?.should be_true
165
165
  end
166
166
 
167
- it "should have a hash with a length from defaults on initialization" do
167
+ it "should have a hash with a length from defaults on initialization." do
168
168
  setting = DummySetting.new
169
169
  setting.as_hash.length.should eq(1)
170
170
  end
171
171
 
172
- it "should mash defaults together with new settings" do
172
+ it "should mash defaults together with new settings." do
173
173
  setting = DummySetting.new(:movie => { :genre => 'asdf' })
174
174
  end
175
175
  end
@@ -181,18 +181,18 @@ describe Defaultable::Settings do
181
181
  end
182
182
  end
183
183
 
184
- it "should not include defaults in the registry" do
184
+ it "should not include defaults in the registry." do
185
185
  setting = DummySetting.new
186
186
  setting.registry.as_hash.has_key?(:movie).should be_false
187
187
  end
188
188
 
189
- it "should not include defaults in the registry unless overwritten" do
189
+ it "should not include defaults in the registry unless overwritten." do
190
190
  setting = DummySetting.new
191
191
  setting.movie = 'asdf'
192
192
  setting.registry.as_hash.has_key?(:movie).should be_false
193
193
  end
194
194
 
195
- it "should be able to overwrite nested defaults" do
195
+ it "should be able to overwrite nested defaults." do
196
196
  class DummySetting < Defaultable::Settings
197
197
  set_defaults :movie => {:name => 'Iron Man', :genre => 'Action Adventure'}
198
198
  end
@@ -203,7 +203,7 @@ describe Defaultable::Settings do
203
203
  setting.registry.as_hash['movie'].has_key?('name').should be_false
204
204
  end
205
205
 
206
- it "should be able to overwrite multiple nested defaults" do
206
+ it "should be able to overwrite multiple nested defaults." do
207
207
  class DummySetting < Defaultable::Settings
208
208
  set_defaults :movie => {:name => 'Iron Man', :genre => 'Action Adventure'}
209
209
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: defaultable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
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: 2011-12-22 00:00:00.000000000 Z
12
+ date: 2011-12-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &70121466071200 !ruby/object:Gem::Requirement
16
+ requirement: &70253939215940 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70121466071200
24
+ version_requirements: *70253939215940
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70121466070520 !ruby/object:Gem::Requirement
27
+ requirement: &70253939215280 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '2.7'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70121466070520
35
+ version_requirements: *70253939215280
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: pry
38
- requirement: &70121466069920 !ruby/object:Gem::Requirement
38
+ requirement: &70253939214780 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,18 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70121466069920
46
+ version_requirements: *70253939214780
47
+ - !ruby/object:Gem::Dependency
48
+ name: awesome_print
49
+ requirement: &70253939214140 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70253939214140
47
58
  description: Defaultable is an extendable class to allow easy method chaining for
48
59
  settings along with defaults.
49
60
  email:
@@ -63,10 +74,10 @@ files:
63
74
  - lib/defaultable/serialization.rb
64
75
  - lib/defaultable/settings.rb
65
76
  - lib/defaultable/version.rb
66
- - spec/defaultable_spec.rb
67
77
  - spec/env_test.yml
68
78
  - spec/registry_spec.rb
69
79
  - spec/serialization_spec.rb
80
+ - spec/settings_spec.rb
70
81
  - spec/spec_helper.rb
71
82
  - spec/test.yml
72
83
  homepage: https://github.com/bobbytables/defaultable
@@ -94,9 +105,9 @@ signing_key:
94
105
  specification_version: 3
95
106
  summary: Settings made easy.
96
107
  test_files:
97
- - spec/defaultable_spec.rb
98
108
  - spec/env_test.yml
99
109
  - spec/registry_spec.rb
100
110
  - spec/serialization_spec.rb
111
+ - spec/settings_spec.rb
101
112
  - spec/spec_helper.rb
102
113
  - spec/test.yml