defaultable 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +47 -1
- data/defaultable.gemspec +2 -1
- data/lib/defaultable/serialization.rb +6 -2
- data/lib/defaultable/settings.rb +4 -0
- data/lib/defaultable/version.rb +1 -1
- data/spec/settings_spec.rb +10 -0
- metadata +22 -11
data/README.md
CHANGED
@@ -36,4 +36,50 @@ end
|
|
36
36
|
|
37
37
|
```
|
38
38
|
|
39
|
-
The second parameter of ```set_defaults``` is the environment variable you'd like to use from the file.
|
39
|
+
The second parameter of ```set_defaults``` is the environment variable you'd like to use from the file. For example "development".
|
40
|
+
|
41
|
+
|
42
|
+
## Serialization
|
43
|
+
|
44
|
+
Sometimes we want to store settings for a database record in the actual schema. To do that, use the ```Defaultable::Serialization``` class.
|
45
|
+
We'll use ```ActiveRecord::Base#serialize``` method in this example.
|
46
|
+
|
47
|
+
First create a model for your settings by extending ```Defaultable::Settings```.
|
48
|
+
|
49
|
+
```
|
50
|
+
class UserSetting < Defaultable::Settings
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
Second, create an initializer. We need this to setup the serialization class for ActiveRecord.
|
55
|
+
|
56
|
+
```
|
57
|
+
Defaultable::Serialization.settings_class = UserSetting
|
58
|
+
```
|
59
|
+
|
60
|
+
Now on your User model, setup the serialization class on it.
|
61
|
+
|
62
|
+
```
|
63
|
+
class User < ActiveRecord::Base
|
64
|
+
serialize :settings, Defaultable::Serialization
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
#### What we just accomplished
|
69
|
+
|
70
|
+
Now we have the ability to setup user settings on creation and updating.
|
71
|
+
|
72
|
+
```
|
73
|
+
user = User.new
|
74
|
+
user.settings.newsletter = true
|
75
|
+
user.save
|
76
|
+
|
77
|
+
|
78
|
+
User.find(1).settings.newsletter # => true
|
79
|
+
```
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
|
data/defaultable.gemspec
CHANGED
@@ -15,9 +15,10 @@ Gem::Specification.new do |gem|
|
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = Defaultable::VERSION
|
17
17
|
|
18
|
-
gem.add_dependency("activesupport", ">= 3.
|
18
|
+
gem.add_dependency("activesupport", ">= 3.2")
|
19
19
|
|
20
20
|
gem.add_development_dependency("rspec", "~> 2.7")
|
21
21
|
gem.add_development_dependency("pry")
|
22
22
|
gem.add_development_dependency("awesome_print")
|
23
|
+
gem.add_development_dependency("activerecord", ">= 3.2")
|
23
24
|
end
|
@@ -18,10 +18,14 @@ module Defaultable
|
|
18
18
|
|
19
19
|
# Called to convert from ruby object to serialized data.
|
20
20
|
def dump(obj)
|
21
|
-
raise TypeError, "Serialization failed: Object is not of type #{self.class.settings_class.name}."
|
21
|
+
raise TypeError, "Serialization failed: Object is not of type #{self.class.settings_class.name}." if !obj.is_a?(self.class.settings_class) && !obj.nil?
|
22
22
|
|
23
23
|
# We need to use the registry because we don't want to store defaults that weren't overwritten
|
24
|
-
|
24
|
+
if obj.nil?
|
25
|
+
self.class.settings_class.new
|
26
|
+
else
|
27
|
+
obj.class.new(obj.registry.as_hash).to_yaml if obj
|
28
|
+
end
|
25
29
|
end
|
26
30
|
|
27
31
|
def raw_load(data)
|
data/lib/defaultable/settings.rb
CHANGED
data/lib/defaultable/version.rb
CHANGED
data/spec/settings_spec.rb
CHANGED
@@ -33,6 +33,16 @@ describe Defaultable::Settings do
|
|
33
33
|
setting.foo?.should be_true
|
34
34
|
end
|
35
35
|
|
36
|
+
it ".empty? should return true" do
|
37
|
+
setting = Defaultable::Settings.new
|
38
|
+
setting.should be_empty
|
39
|
+
end
|
40
|
+
|
41
|
+
it ".empty? should return false" do
|
42
|
+
setting = Defaultable::Settings.new(:foo => 'bar')
|
43
|
+
setting.should_not be_empty
|
44
|
+
end
|
45
|
+
|
36
46
|
describe "Defaults." do
|
37
47
|
it "should have default settings." do
|
38
48
|
Defaultable::Settings.set_defaults File.expand_path('../', __FILE__) + '/test.yml'
|
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.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,22 +9,22 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-03-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &70100058426160 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: '3.
|
21
|
+
version: '3.2'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70100058426160
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70100058423640 !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: *
|
35
|
+
version_requirements: *70100058423640
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: pry
|
38
|
-
requirement: &
|
38
|
+
requirement: &70100058421280 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70100058421280
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: awesome_print
|
49
|
-
requirement: &
|
49
|
+
requirement: &70100058437380 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,7 +54,18 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70100058437380
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: activerecord
|
60
|
+
requirement: &70100058432520 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '3.2'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70100058432520
|
58
69
|
description: Defaultable is an extendable class to allow easy method chaining for
|
59
70
|
settings along with defaults.
|
60
71
|
email:
|