property_sets 2.7.0 → 2.8.0
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.
- checksums.yaml +4 -4
- data/lib/property_sets/active_record_extension.rb +114 -107
- data/lib/property_sets/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9d4b2d253e7158b77c84e26addaa5b677b842e56
|
4
|
+
data.tar.gz: 7e306e74ae2ab092896997bd081384555d2f3ef4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
35
|
-
|
36
|
-
|
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
|
-
|
42
|
-
|
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
|
-
|
54
|
-
|
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
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
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
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
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
|
-
|
70
|
-
|
71
|
-
|
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
|
77
|
-
|
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
|
-
|
82
|
-
|
83
|
-
|
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
|
-
|
86
|
-
|
87
|
-
|
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
|
-
|
90
|
-
|
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
|
-
|
94
|
-
|
95
|
-
|
94
|
+
def save(*args)
|
95
|
+
each { |p| p.save(*args) }
|
96
|
+
end
|
96
97
|
|
97
|
-
|
98
|
-
|
99
|
-
|
98
|
+
def save!(*args)
|
99
|
+
each { |p| p.save!(*args) }
|
100
|
+
end
|
100
101
|
|
101
|
-
|
102
|
-
|
103
|
-
|
102
|
+
def protected?(arg)
|
103
|
+
lookup(arg).protected?
|
104
|
+
end
|
104
105
|
|
105
|
-
|
106
|
-
|
107
|
-
|
106
|
+
def enable(arg)
|
107
|
+
send("#{arg}=", "1")
|
108
|
+
end
|
108
109
|
|
109
|
-
|
110
|
-
|
111
|
-
|
110
|
+
def disable(arg)
|
111
|
+
send("#{arg}=", "0")
|
112
|
+
end
|
112
113
|
|
113
|
-
|
114
|
-
|
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
|
-
|
130
|
-
|
131
|
-
|
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
|
-
|
122
|
+
def lookup_value(type, key)
|
123
|
+
serialized = property_serialized?(key)
|
136
124
|
|
137
|
-
|
138
|
-
|
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
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
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
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
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
|
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.
|
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-
|
11
|
+
date: 2014-11-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|