simple_model 1.2.17 → 1.2.18
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 +8 -8
- data/Gemfile +1 -3
- data/gemfiles/3.0.gemfile +0 -2
- data/gemfiles/3.1.gemfile +0 -2
- data/gemfiles/3.2.gemfile +0 -2
- data/lib/simple_model/attributes.rb +96 -70
- data/lib/simple_model/extend_core.rb +2 -1
- data/lib/simple_model/version.rb +1 -1
- data/spec/attributes_spec.rb +7 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZGI2YjA2MjE0MTA1YTgyYjQyOTg4ZTk2YjI5MmE1MThlNjY4NDFjOA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YWIxNDkwOTRkNjhjM2UxN2Q0ODQzY2JlN2VjMGE2ODAzOTQ5YmNjMg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YzFhZTg3ZGZiNTJhMTkwMjEwZjFlOTIwMzRlNWVlMDExYmY2NjYzYzdjYTk3
|
10
|
+
M2IzM2M1NzFjNWU1NGY4ZTk4NDBkMGI2N2FkYzhmN2M2NjRhZDgzMDZlN2Ji
|
11
|
+
MzY5MjY0MGQxMjAxYjg4ZWUzMTgzZjYwYWQ4MmU0N2Q1NjJlNjI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MGIzMThlMGJlNzZkOGVhNDZlZTgwMTc4MWRmMTVjNzBiYmY0YTZiZDdkNzRh
|
14
|
+
M2JhNWI3NmJlODFjNjZiNjgxOWUzMmEyMjA2ZjY0YjM5ODYzNTBlNDJhOTli
|
15
|
+
NDg4N2E0ZmI4ZTczNTY4NWQzNTM3ZWY5ODUyOGUyNTgxMmRkM2I=
|
data/Gemfile
CHANGED
data/gemfiles/3.0.gemfile
CHANGED
data/gemfiles/3.1.gemfile
CHANGED
data/gemfiles/3.2.gemfile
CHANGED
@@ -3,7 +3,8 @@ module SimpleModel
|
|
3
3
|
include ExtendCore
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
include ActiveModel::AttributeMethods
|
6
|
-
|
6
|
+
attr_accessor :attributes
|
7
|
+
|
7
8
|
def initialize(*attrs)
|
8
9
|
attrs = attrs.extract_options!
|
9
10
|
attrs = attributes_with_for_init(attrs)
|
@@ -12,17 +13,13 @@ module SimpleModel
|
|
12
13
|
self.class.after_initialize.call(self) if self.class.after_initialize
|
13
14
|
end
|
14
15
|
|
15
|
-
# Returns true if attribute has been initialized
|
16
|
-
def initialized?(attr)
|
17
|
-
attributes.key?(attr.to_sym)
|
18
|
-
end
|
19
|
-
|
20
16
|
def attributes
|
21
17
|
@attributes ||= HashWithIndifferentAccess.new
|
22
18
|
end
|
23
19
|
|
24
|
-
|
25
|
-
|
20
|
+
# Returns true if attribute has been initialized
|
21
|
+
def initialized?(attr)
|
22
|
+
self.attributes.key?(attr.to_sym)
|
26
23
|
end
|
27
24
|
|
28
25
|
def get(attr)
|
@@ -39,8 +36,47 @@ module SimpleModel
|
|
39
36
|
end
|
40
37
|
alias :set_attributes :set
|
41
38
|
|
39
|
+
def set_attribute(attr,val)
|
40
|
+
options = self.class.defined_attributes[attr] || {}
|
41
|
+
if allow_attribute_action?(self,val,options)
|
42
|
+
val = fetch_default_value(options[:default]) if (!options[:allow_blank] && options.key?(:default) && val.blank?)
|
43
|
+
val = options[:on_set].call(self,val) unless (!options.key?(:on_set) || (val.blank? && !options[:allow_blank]) )
|
44
|
+
will_change = "#{attr}_will_change!".to_sym
|
45
|
+
self.send(will_change) if (initialized?(attr) && val != self.attributes[attr])
|
46
|
+
self.attributes[attr] = val
|
47
|
+
options[:after_set].call(self,val) if options[:after_set]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def get_attribute(attr)
|
52
|
+
val = self.attributes[attr]
|
53
|
+
options = self.class.defined_attributes[attr] || {}
|
54
|
+
if (options.key?(:default) && (!self.initialized?(attr) || (!options[:allow_blank] && val.blank?)))
|
55
|
+
val = self.attributes[attr] = fetch_default_value(options[:default])
|
56
|
+
end
|
57
|
+
if options[:on_get]
|
58
|
+
options[:on_get].call(self,val)
|
59
|
+
else
|
60
|
+
val
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def get_attribute?(attr)
|
65
|
+
val = get_attribute(attr)
|
66
|
+
if val.respond_to?(:to_b)
|
67
|
+
val = val.to_b
|
68
|
+
else
|
69
|
+
val = !val.blank? if val.respond_to?(:blank?)
|
70
|
+
end
|
71
|
+
val
|
72
|
+
end
|
73
|
+
|
42
74
|
private
|
43
75
|
|
76
|
+
def attribute_defined?(attr)
|
77
|
+
self.class.attribute_defined?(attr)
|
78
|
+
end
|
79
|
+
|
44
80
|
def fetch_default_value(arg)
|
45
81
|
return self.send(arg) if (arg.is_a?(Symbol) && self.respond_to?(arg))
|
46
82
|
arg
|
@@ -58,12 +94,12 @@ module SimpleModel
|
|
58
94
|
d
|
59
95
|
end
|
60
96
|
|
97
|
+
# Only set default if there is a default value, initializing is allow and
|
98
|
+
# new attributes do not have a value to set and
|
61
99
|
def allow_set_default?(d,k,v)
|
62
|
-
(v[:default] && v[:initialize]
|
100
|
+
(v[:default] && (v[:initialize] != false) && (!d.key?(k) && !d.key?(self.class.alias_attributes[k])))
|
63
101
|
end
|
64
102
|
|
65
|
-
private
|
66
|
-
|
67
103
|
def allow_attribute_action?(obj,val,options)
|
68
104
|
return true if (options[:if].blank? && options[:unless].blank?)
|
69
105
|
b = true
|
@@ -92,6 +128,31 @@ module SimpleModel
|
|
92
128
|
end
|
93
129
|
|
94
130
|
module ClassMethods
|
131
|
+
DEFAULT_ATTRIBUTE_SETTINGS = {:attributes_method => :attributes,
|
132
|
+
:allow_blank => true,
|
133
|
+
:initialize => true
|
134
|
+
}.freeze
|
135
|
+
|
136
|
+
AVAILABLE_ATTRIBUTE_METHODS = {
|
137
|
+
:has_attribute => {:alias => :has_attributes},
|
138
|
+
:has_boolean => {:cast_to => :to_b, :alias => :has_booleans},
|
139
|
+
:has_currency => {:cast_to => :to_d, :alias => :has_currencies},
|
140
|
+
:has_date => {:cast_to => :to_date, :alias => :has_dates} ,
|
141
|
+
:has_decimal => {:cast_to => :to_d, :alias => :has_decimals},
|
142
|
+
:has_float => {:cast_to => :to_f, :alias => :has_floats},
|
143
|
+
:has_int => {:cast_to => :to_i, :alias => :has_ints},
|
144
|
+
:has_time => {:cast_to => :to_time, :alias => :has_times}
|
145
|
+
}.freeze
|
146
|
+
|
147
|
+
AVAILABLE_ATTRIBUTE_METHODS.each do |method,method_options|
|
148
|
+
define_method(method) do |*attributes|
|
149
|
+
options = default_attribute_settings.merge(attributes.extract_options!)
|
150
|
+
options[:on_set] = lambda {|obj,val| val.send(method_options[:cast_to]) } if method_options[:cast_to]
|
151
|
+
create_attribute_methods(attributes,options)
|
152
|
+
end
|
153
|
+
module_eval("alias #{method_options[:alias]} #{method}")
|
154
|
+
end
|
155
|
+
|
95
156
|
# Creates a new instance where the attributes store is set to object
|
96
157
|
# provided, which allows one to pass a session store hash or any other
|
97
158
|
# hash-like object to be used for persistence. Typically used for modeling
|
@@ -106,10 +167,10 @@ module SimpleModel
|
|
106
167
|
# end
|
107
168
|
#
|
108
169
|
def new_with_store(session_hash)
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
170
|
+
nw = self.new()
|
171
|
+
nw.attributes = session_hash
|
172
|
+
nw.set(nw.send(:attributes_with_for_init,session_hash))
|
173
|
+
nw
|
113
174
|
end
|
114
175
|
|
115
176
|
def alias_attributes
|
@@ -141,12 +202,7 @@ module SimpleModel
|
|
141
202
|
# ** If :initialize is set to false you must set :allow_blank to false or it will never set the default value
|
142
203
|
# * :allow_blank - when set to false, if an attributes value is blank attempts to set the default value, defaults to true
|
143
204
|
def default_attribute_settings
|
144
|
-
@default_attribute_settings ||=
|
145
|
-
:on_set => lambda {|obj,attr| attr},
|
146
|
-
:on_get => lambda {|obj,attr| attr},
|
147
|
-
:allow_blank => true,
|
148
|
-
:initialize => true
|
149
|
-
}
|
205
|
+
@default_attribute_settings ||= DEFAULT_ATTRIBUTE_SETTINGS
|
150
206
|
end
|
151
207
|
|
152
208
|
def default_attribute_settings=default_attribute_settings
|
@@ -159,38 +215,33 @@ module SimpleModel
|
|
159
215
|
def add_defined_attribute(attr,options)
|
160
216
|
self.defined_attributes[attr] = options
|
161
217
|
@attribute_methods_generated = nil #if (ActiveModel::VERSION::MAJOR == 3 && ActiveModel::VERSION::MINOR == 0)
|
162
|
-
define_attribute_methods(
|
218
|
+
define_attribute_methods(defined_attributes_keys)
|
219
|
+
end
|
220
|
+
|
221
|
+
# We don't want to call define_attribute_methods on methods defined in the parent class
|
222
|
+
def defined_attributes_keys
|
223
|
+
dak = self.defined_attributes.keys
|
224
|
+
dak = dak - self.superclass.defined_attributes.keys if self.superclass.respond_to?(:defined_attributes)
|
225
|
+
dak
|
163
226
|
end
|
164
227
|
|
165
228
|
# builds the setter and getter methods
|
166
229
|
def create_attribute_methods(attributes,options)
|
167
230
|
unless attributes.blank?
|
168
231
|
attributes.each do |attr|
|
169
|
-
define_reader_with_options(attr,options)
|
170
232
|
define_setter_with_options(attr,options)
|
233
|
+
define_reader_with_options(attr,options)
|
234
|
+
add_defined_attribute(attr,options)
|
171
235
|
end
|
172
236
|
end
|
173
237
|
end
|
174
238
|
|
175
239
|
def define_reader_with_options(attr,options)
|
176
|
-
add_defined_attribute(attr,options)
|
177
|
-
options = default_attribute_settings.merge(options) if options[:on_get].blank?
|
178
240
|
define_method(attr) do
|
179
|
-
|
180
|
-
if (options.key?(:default) && (!self.initialized?(attr) || (!options[:allow_blank] && val.blank?)))
|
181
|
-
val = self.attributes[attr] = fetch_default_value(options[:default])
|
182
|
-
end
|
183
|
-
options[:on_get].call(self,val)
|
241
|
+
get_attribute(attr)
|
184
242
|
end
|
185
243
|
define_method("#{attr.to_s}?") do
|
186
|
-
|
187
|
-
val = self.send(attr)
|
188
|
-
if val.respond_to?(:to_b)
|
189
|
-
val = val.to_b
|
190
|
-
else
|
191
|
-
val = !val.blank? if val.respond_to?(:blank?)
|
192
|
-
end
|
193
|
-
val
|
244
|
+
get_attribute?(attr)
|
194
245
|
end
|
195
246
|
end
|
196
247
|
|
@@ -198,38 +249,9 @@ module SimpleModel
|
|
198
249
|
# On set, it will mark the attribute as changed if the attributes has been
|
199
250
|
# initialized.
|
200
251
|
def define_setter_with_options(attr,options)
|
201
|
-
add_defined_attribute(attr,options)
|
202
|
-
options = default_attribute_settings.merge(options) if (options[:on_set].blank? || options[:after_set].blank?)
|
203
252
|
define_method("#{attr.to_s}=") do |val|
|
204
|
-
|
205
|
-
val = fetch_default_value(options[:default]) if (!options[:allow_blank] && options.key?(:default) && val.blank?)
|
206
|
-
val = options[:on_set].call(self,val) unless (val.blank? && !options[:allow_blank] )
|
207
|
-
will_change = "#{attr}_will_change!".to_sym
|
208
|
-
self.send(will_change) if (initialized?(attr) && val != self.attributes[attr])
|
209
|
-
self.attributes[attr] = val
|
210
|
-
options[:after_set].call(self,val) if options[:after_set]
|
211
|
-
end
|
212
|
-
end
|
213
|
-
end
|
214
|
-
|
215
|
-
AVAILABLE_ATTRIBUTE_METHODS = {
|
216
|
-
:has_attribute => {:alias => :has_attributes},
|
217
|
-
:has_boolean => {:cast_to => :to_b, :alias => :has_booleans},
|
218
|
-
:has_currency => {:cast_to => :to_d, :alias => :has_currencies},
|
219
|
-
:has_date => {:cast_to => :to_date, :alias => :has_dates} ,
|
220
|
-
:has_decimal => {:cast_to => :to_d, :alias => :has_decimals},
|
221
|
-
:has_float => {:cast_to => :to_f, :alias => :has_floats},
|
222
|
-
:has_int => {:cast_to => :to_i, :alias => :has_ints},
|
223
|
-
:has_time => {:cast_to => :to_time, :alias => :has_times}
|
224
|
-
}
|
225
|
-
|
226
|
-
AVAILABLE_ATTRIBUTE_METHODS.each do |method,method_options|
|
227
|
-
define_method(method) do |*attributes|
|
228
|
-
options = default_attribute_settings.merge(attributes.extract_options!)
|
229
|
-
options[:on_set] = lambda {|obj,val| val.send(method_options[:cast_to]) } if method_options[:cast_to]
|
230
|
-
create_attribute_methods(attributes,options)
|
253
|
+
set_attribute(attr,val)
|
231
254
|
end
|
232
|
-
module_eval("alias #{method_options[:alias]} #{method}")
|
233
255
|
end
|
234
256
|
|
235
257
|
# Creates alias setter and getter for the supplied attribute using the supplied alias
|
@@ -239,6 +261,9 @@ module SimpleModel
|
|
239
261
|
define_method(new_alias) do
|
240
262
|
self.send(attribute)
|
241
263
|
end
|
264
|
+
define_method("#{new_alias}?") do
|
265
|
+
self.send("#{attribute}?")
|
266
|
+
end
|
242
267
|
define_method("#{new_alias.to_s}=") do |*args, &block|
|
243
268
|
self.send("#{attribute.to_s}=",*args, &block)
|
244
269
|
end
|
@@ -280,7 +305,8 @@ module SimpleModel
|
|
280
305
|
# hack to keep things working when a class inherits from a super that
|
281
306
|
# has ActiveModel::Dirty included
|
282
307
|
def inherited(base)
|
283
|
-
base.
|
308
|
+
base.defined_attributes = self.defined_attributes.dup
|
309
|
+
base.alias_attributes = self.alias_attributes.dup
|
284
310
|
super
|
285
311
|
# Rails 3.0 Hack
|
286
312
|
if (ActiveModel::VERSION::MAJOR == 3 && ActiveModel::VERSION::MINOR == 0)
|
@@ -288,7 +314,7 @@ module SimpleModel
|
|
288
314
|
base.attribute_method_affix :prefix => 'reset_', :suffix => '!'
|
289
315
|
end
|
290
316
|
end
|
291
|
-
end
|
317
|
+
end # end ClassMethods
|
292
318
|
|
293
319
|
# Rails 3.0 does some weird stuff with ActiveModel::Dirty so we need a
|
294
320
|
# hack to keep things working when a class includes a module that has
|
@@ -60,7 +60,8 @@ module SimpleModel
|
|
60
60
|
|
61
61
|
# returns boolean value for common boolean string values
|
62
62
|
def to_b
|
63
|
-
|
63
|
+
return true if self =~ (/^(true|t|yes|y|1)$/i)
|
64
|
+
false
|
64
65
|
end
|
65
66
|
|
66
67
|
# Takes known US formatted date/time strings (MM/DD/YYYY TIME) and converts
|
data/lib/simple_model/version.rb
CHANGED
data/spec/attributes_spec.rb
CHANGED
@@ -67,10 +67,10 @@ describe SimpleModel::Attributes do
|
|
67
67
|
context '#new_with_store'do
|
68
68
|
it "should use the provided object as the attribute store" do
|
69
69
|
my_store = {:test1 => 1,:test2 => 2}
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
my_store[:test1].should eql(
|
70
|
+
nw = TestInit.new_with_store(my_store)
|
71
|
+
nw.test1 = 3
|
72
|
+
nw.test1.should eql(3)
|
73
|
+
my_store[:test1].should eql(nw.test1)
|
74
74
|
end
|
75
75
|
end
|
76
76
|
|
@@ -251,12 +251,10 @@ describe SimpleModel::Attributes do
|
|
251
251
|
n.respond_to?(:bar_will_change!).should eql(true)
|
252
252
|
end
|
253
253
|
|
254
|
-
it "should defaults that were not initialized should work from parent class" do
|
254
|
+
it "should set defaults that were not initialized should work from parent class" do
|
255
255
|
n = NewerBase.new
|
256
|
-
n.some.should eql(
|
257
|
-
n.
|
258
|
-
n.some.should be_a(Date)
|
259
|
-
n.thing.should eql(Date.today)
|
256
|
+
n.some.should eql(n.send(:fetch_date))
|
257
|
+
n.thing.should eql(n.send(:fetch_date))
|
260
258
|
end
|
261
259
|
|
262
260
|
it "should allow redefining methods in child classes" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.18
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua T Mckinney
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10
|
11
|
+
date: 2014-11-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|