property_sets 2.7.0 → 2.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 922f8ba1f096e2acbdaca1c1306c45ae85dee3f7
4
- data.tar.gz: affb41af60814521310b3647628be982ae5a107c
3
+ metadata.gz: 9d4b2d253e7158b77c84e26addaa5b677b842e56
4
+ data.tar.gz: 7e306e74ae2ab092896997bd081384555d2f3ef4
5
5
  SHA512:
6
- metadata.gz: d1995e3d9882e48331dc115f198e2fe1fb9526c5166d56e324e190d39076a5b5b90784105ed7cd91c7556fa5f0899dbddba21a779e426411e48f923e45916812
7
- data.tar.gz: acb0c651d10ab7c8283d46861b06306e635c43f542e17e5a4608df0220a631ea291280d61aec4e706620a48db9235333a0458c561edda513477c7721c636a274
6
+ metadata.gz: 3c51d22c6ab9fbc8c62dea9ae73915db6c712bf7bbe4128e73112835181bcd345ebb0c621130aca9efca1a76aba56157a68f243adaf78ca82be21f441d6d937d
7
+ data.tar.gz: 81251ba42e291441fa592c12c8dcbc709ba7d746a6b22680d9efe1f27af1cf13acdbade150e0ec426e3aa1176b1d9df63e8eea3a6911442421181c8feb319217
@@ -22,139 +22,146 @@ module PropertySets
22
22
  property_class.instance_eval(&block)
23
23
 
24
24
  hash_opts = {:class_name => property_class.name, :autosave => true, :dependent => :destroy}.merge(options)
25
- has_many association, hash_opts do
26
- # Accepts an array of names as strings or symbols and returns a hash.
27
- def get(keys = [])
28
- property_keys = if keys.empty?
29
- association_class.keys
30
- else
31
- association_class.keys & keys.map(&:to_s)
32
- end
33
25
 
34
- property_pairs = property_keys.map do |name|
35
- value = lookup_value(association_class.type(name), name)
36
- [name, value]
37
- end.flatten(1)
38
- HashWithIndifferentAccess[*property_pairs]
39
- end
26
+ silence_warnings do
27
+ has_many association, hash_opts do
28
+ include PropertySets::ActiveRecordExtension::AssociationExtensions
40
29
 
41
- # Accepts a name value pair hash { :name => 'value', :pairs => true } and builds a property for each key
42
- def set(property_pairs, with_protection = false)
43
- property_pairs.keys.each do |name|
44
- record = lookup(name)
45
- if with_protection && record.protected?
46
- logger.warn("Someone tried to update the protected #{name} property to #{property_pairs[name]}")
47
- else
48
- send("#{name}=", property_pairs[name])
49
- end
50
- end
51
- end
30
+ property_class.keys.each do |key|
31
+ raise "Invalid property key #{key}" if self.respond_to?(key)
52
32
 
53
- property_class.keys.each do |key|
54
- raise "Invalid property key #{key}" if self.respond_to?(key)
33
+ # Reports the coerced truth value of the property
34
+ define_method "#{key}?" do
35
+ type = property_class.type(key)
36
+ value = lookup_value(type, key)
37
+ ![ "false", "0", "", "off", "n" ].member?(value.to_s.downcase)
38
+ end
55
39
 
56
- # Reports the coerced truth value of the property
57
- define_method "#{key}?" do
58
- type = property_class.type(key)
59
- value = lookup_value(type, key)
60
- ![ "false", "0", "", "off", "n" ].member?(value.to_s.downcase)
61
- end
40
+ # Returns the value of the property
41
+ define_method "#{key}" do
42
+ type = property_class.type(key)
43
+ lookup_value(type, key)
44
+ end
62
45
 
63
- # Returns the value of the property
64
- define_method "#{key}" do
65
- type = property_class.type(key)
66
- lookup_value(type, key)
67
- end
46
+ # Assigns a new value to the property
47
+ define_method "#{key}=" do |value|
48
+ instance = lookup(key)
49
+ instance.value = PropertySets::Casting.write(property_class.type(key), value)
50
+ instance.value
51
+ end
68
52
 
69
- # Assigns a new value to the property
70
- define_method "#{key}=" do |value|
71
- instance = lookup(key)
72
- instance.value = PropertySets::Casting.write(property_class.type(key), value)
73
- instance.value
53
+ define_method "#{key}_record" do
54
+ lookup(key)
55
+ end
74
56
  end
75
57
 
76
- define_method "#{key}_record" do
77
- lookup(key)
58
+ define_method :property_serialized? do |key|
59
+ property_class.type(key) == :serialized
78
60
  end
79
61
  end
62
+ end
63
+ end
64
+ end
80
65
 
81
- define_method :property_serialized? do |key|
82
- property_class.type(key) == :serialized
83
- end
66
+ module AssociationExtensions
67
+ # Accepts an array of names as strings or symbols and returns a hash.
68
+ def get(keys = [])
69
+ property_keys = if keys.empty?
70
+ association_class.keys
71
+ else
72
+ association_class.keys & keys.map(&:to_s)
73
+ end
84
74
 
85
- def save(*args)
86
- each { |p| p.save(*args) }
87
- end
75
+ property_pairs = property_keys.map do |name|
76
+ value = lookup_value(association_class.type(name), name)
77
+ [name, value]
78
+ end.flatten(1)
79
+ HashWithIndifferentAccess[*property_pairs]
80
+ end
88
81
 
89
- def save!(*args)
90
- each { |p| p.save!(*args) }
82
+ # Accepts a name value pair hash { :name => 'value', :pairs => true } and builds a property for each key
83
+ def set(property_pairs, with_protection = false)
84
+ property_pairs.keys.each do |name|
85
+ record = lookup(name)
86
+ if with_protection && record.protected?
87
+ logger.warn("Someone tried to update the protected #{name} property to #{property_pairs[name]}")
88
+ else
89
+ send("#{name}=", property_pairs[name])
91
90
  end
91
+ end
92
+ end
92
93
 
93
- def protected?(arg)
94
- lookup(arg).protected?
95
- end
94
+ def save(*args)
95
+ each { |p| p.save(*args) }
96
+ end
96
97
 
97
- def enable(arg)
98
- send("#{arg}=", "1")
99
- end
98
+ def save!(*args)
99
+ each { |p| p.save!(*args) }
100
+ end
100
101
 
101
- def disable(arg)
102
- send("#{arg}=", "0")
103
- end
102
+ def protected?(arg)
103
+ lookup(arg).protected?
104
+ end
104
105
 
105
- def build_default(arg)
106
- build(:name => arg.to_s, :value => raw_default(arg))
107
- end
106
+ def enable(arg)
107
+ send("#{arg}=", "1")
108
+ end
108
109
 
109
- def lookup_without_default(arg)
110
- detect { |property| property.name.to_sym == arg.to_sym }
111
- end
110
+ def disable(arg)
111
+ send("#{arg}=", "0")
112
+ end
112
113
 
113
- def lookup_value(type, key)
114
- serialized = property_serialized?(key)
115
-
116
- if instance = lookup_without_default(key)
117
- instance.value_serialized = serialized
118
- PropertySets::Casting.read(type, instance.value)
119
- else
120
- value = default(key)
121
- if serialized
122
- PropertySets::Casting.deserialize(value)
123
- else
124
- PropertySets::Casting.read(type, value)
125
- end
126
- end
127
- end
114
+ def build_default(arg)
115
+ build(:name => arg.to_s, :value => raw_default(arg))
116
+ end
128
117
 
129
- # The finder method which returns the property if present, otherwise a new instance with defaults
130
- def lookup(arg)
131
- instance = lookup_without_default(arg)
132
- instance ||= build_default(arg)
133
- instance.value_serialized = property_serialized?(arg)
118
+ def lookup_without_default(arg)
119
+ detect { |property| property.name.to_sym == arg.to_sym }
120
+ end
134
121
 
135
- owner = proxy_association.owner
122
+ def lookup_value(type, key)
123
+ serialized = property_serialized?(key)
136
124
 
137
- instance.send("#{owner_class_sym}=", owner) if owner.new_record?
138
- instance
125
+ if instance = lookup_without_default(key)
126
+ instance.value_serialized = serialized
127
+ PropertySets::Casting.read(type, instance.value)
128
+ else
129
+ value = default(key)
130
+ if serialized
131
+ PropertySets::Casting.deserialize(value)
132
+ else
133
+ PropertySets::Casting.read(type, value)
139
134
  end
135
+ end
136
+ end
140
137
 
141
- # This finder method returns the property if present, otherwise a new instance with the default value.
142
- # It does not have the side effect of adding a new setting object.
143
- def lookup_or_default(arg)
144
- instance = lookup_without_default(arg)
145
- instance ||= association_class.new(:value => raw_default(arg))
146
- instance.value_serialized = property_serialized?(arg)
147
- instance
148
- end
138
+ # The finder method which returns the property if present, otherwise a new instance with defaults
139
+ def lookup(arg)
140
+ instance = lookup_without_default(arg)
141
+ instance ||= build_default(arg)
142
+ instance.value_serialized = property_serialized?(arg)
149
143
 
150
- def association_class
151
- @association_class ||= begin
152
- if ActiveRecord::VERSION::STRING >= "3.1.0"
153
- proxy_association.klass
154
- else
155
- @reflection.klass
156
- end
157
- end
144
+ owner = proxy_association.owner
145
+
146
+ instance.send("#{owner_class_sym}=", owner) if owner.new_record?
147
+ instance
148
+ end
149
+
150
+ # This finder method returns the property if present, otherwise a new instance with the default value.
151
+ # It does not have the side effect of adding a new setting object.
152
+ def lookup_or_default(arg)
153
+ instance = lookup_without_default(arg)
154
+ instance ||= association_class.new(:value => raw_default(arg))
155
+ instance.value_serialized = property_serialized?(arg)
156
+ instance
157
+ end
158
+
159
+ def association_class
160
+ @association_class ||= begin
161
+ if ActiveRecord::VERSION::STRING >= "3.1.0"
162
+ proxy_association.klass
163
+ else
164
+ @reflection.klass
158
165
  end
159
166
  end
160
167
  end
@@ -1,3 +1,3 @@
1
1
  module PropertySets
2
- VERSION = "2.7.0"
2
+ VERSION = "2.8.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: property_sets
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.0
4
+ version: 2.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Morten Primdahl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-31 00:00:00.000000000 Z
11
+ date: 2014-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord