property_sets 0.0.10 → 0.0.11
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/README.rdoc +19 -3
- data/VERSION +1 -1
- data/lib/property_sets/active_record_extension.rb +1 -4
- data/lib/property_sets/property_set_model.rb +15 -0
- data/property_sets.gemspec +1 -1
- data/test/test_property_sets.rb +17 -0
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -11,6 +11,7 @@ You configure the allowed stored properties by specifying these in an initialize
|
|
11
11
|
class Account < ActiveRecord::Base
|
12
12
|
property_set :settings do
|
13
13
|
property :version, :default => "v1.0"
|
14
|
+
property :featured
|
14
15
|
property :product
|
15
16
|
end
|
16
17
|
|
@@ -21,9 +22,24 @@ You configure the allowed stored properties by specifying these in an initialize
|
|
21
22
|
|
22
23
|
The declared properties can then be accessed runtime via the defined association:
|
23
24
|
|
24
|
-
|
25
|
-
account.settings.
|
26
|
-
|
25
|
+
# Return the version record for this account - or a new record if none exists
|
26
|
+
account.settings.version
|
27
|
+
|
28
|
+
# Create (or update) the version record with default value
|
29
|
+
account.settings.version.create
|
30
|
+
|
31
|
+
# Create (or update) the version record with the given value
|
32
|
+
account.settings.version.create(:value => "v1.1")
|
33
|
+
|
34
|
+
# Destroy the version record
|
35
|
+
account.settings.version.destroy
|
36
|
+
|
37
|
+
On top of the basic access paths, there are some short cuts:
|
38
|
+
|
39
|
+
account.settings.featured=(1) # immediately changes the value of the setting
|
40
|
+
account.settings.featured? # coerces the setting to boolean AR style
|
41
|
+
account.settings.featured.enable # sets the value of this setting to a true value
|
42
|
+
account.settings.featured.disable # sets the value of this setting to a false value
|
27
43
|
|
28
44
|
If the value has never been set, a nil (or default) is returned. And that's pretty much it.
|
29
45
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.11
|
@@ -73,12 +73,9 @@ module PropertySets
|
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
|
-
def self.included(receiver)
|
77
|
-
receiver.extend(ClassMethods)
|
78
|
-
end
|
79
76
|
end
|
80
77
|
end
|
81
78
|
|
82
79
|
ActiveRecord::Base.class_eval do
|
83
|
-
|
80
|
+
extend PropertySets::ActiveRecordExtension::ClassMethods
|
84
81
|
end
|
@@ -10,6 +10,16 @@ module PropertySets
|
|
10
10
|
!false?
|
11
11
|
end
|
12
12
|
|
13
|
+
def enable
|
14
|
+
update_attribute(:value, "1")
|
15
|
+
self
|
16
|
+
end
|
17
|
+
|
18
|
+
def disable
|
19
|
+
update_attribute(:value, "0")
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
13
23
|
def to_s
|
14
24
|
value.to_s
|
15
25
|
end
|
@@ -24,6 +34,10 @@ module PropertySets
|
|
24
34
|
end
|
25
35
|
end
|
26
36
|
|
37
|
+
def coerce_value
|
38
|
+
self.value = value.to_s unless value.nil?
|
39
|
+
end
|
40
|
+
|
27
41
|
def owner_class_instance
|
28
42
|
send(self.class.owner_class_sym)
|
29
43
|
end
|
@@ -42,6 +56,7 @@ module PropertySets
|
|
42
56
|
base.after_create :reset_owner_association
|
43
57
|
base.after_destroy :reset_owner_association
|
44
58
|
base.validate :validate_format_of_name
|
59
|
+
base.before_create :coerce_value
|
45
60
|
end
|
46
61
|
|
47
62
|
def property(key, options = nil)
|
data/property_sets.gemspec
CHANGED
data/test/test_property_sets.rb
CHANGED
@@ -97,6 +97,23 @@ class TestPropertySets < ActiveSupport::TestCase
|
|
97
97
|
assert !@account.settings.foo?
|
98
98
|
end
|
99
99
|
|
100
|
+
should "support enable/disable semantics" do
|
101
|
+
assert !@account.settings.foo?
|
102
|
+
assert @account.settings.foo.id.nil?
|
103
|
+
@account.settings.foo.enable
|
104
|
+
assert @account.settings.foo.id.present?
|
105
|
+
assert @account.settings.foo?
|
106
|
+
@account.settings.foo.disable
|
107
|
+
assert !@account.settings.foo?
|
108
|
+
end
|
109
|
+
|
110
|
+
should "coerce everything but nil to string" do
|
111
|
+
assert @account.settings.foo.create(:value => 3)
|
112
|
+
assert @account.settings.foo.value == "3"
|
113
|
+
assert @account.settings.foo.create(:value => nil)
|
114
|
+
assert @account.settings.foo.value.nil?
|
115
|
+
end
|
116
|
+
|
100
117
|
should "support bulk build multiple properties in one go" do
|
101
118
|
@account.settings.bulk(:foo => "123", :bar => "456")
|
102
119
|
@account.save!
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: property_sets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 9
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 11
|
10
|
+
version: 0.0.11
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Morten Primdahl
|