simple_model 1.4.1 → 1.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/simple_model/attributes.rb +8 -26
- data/lib/simple_model/base.rb +13 -1
- data/lib/simple_model/version.rb +1 -1
- data/spec/simple_model/base_spec.rb +13 -3
- 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: 40145db9483512a0a6d4209605362c68fdac9679
|
4
|
+
data.tar.gz: aaac8adad7bcbba5e3a08058c2b8b8a6df19317f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ab3cf5ed3c7a3213ba5bb2853da12181015e7c6a6110eebf5fb72d3043c2a3686636b3cc29431a44a458558f092498a4e8407cb35eafbe1f45d5b12cd5c35f14
|
7
|
+
data.tar.gz: cd6bcd5f4e94bd2e8985a62179026401ce55efe8cc5d01359021463d352ee207d35ddb509d673702b659d6c53b79ca1a8e3e7bff88eb4fbaa50b057e32171804
|
@@ -229,7 +229,13 @@ module SimpleModel
|
|
229
229
|
end
|
230
230
|
|
231
231
|
module ClassMethods
|
232
|
-
|
232
|
+
attr_writer :config,
|
233
|
+
:defined_attributes,
|
234
|
+
:alias_attributes,
|
235
|
+
:default_attribute_settings
|
236
|
+
|
237
|
+
attr_reader :before_initialize,
|
238
|
+
:after_initialize
|
233
239
|
|
234
240
|
AVAILABLE_ATTRIBUTE_METHODS.each do |method,method_options|
|
235
241
|
define_method(method) do |*attributes|
|
@@ -263,18 +269,10 @@ module SimpleModel
|
|
263
269
|
@alias_attributes ||= Hash.new
|
264
270
|
end
|
265
271
|
|
266
|
-
def alias_attributes=alias_attributes
|
267
|
-
@alias_attributes = alias_attributes
|
268
|
-
end
|
269
|
-
|
270
272
|
def defined_attributes
|
271
273
|
@defined_attributes ||= Hash.new
|
272
274
|
end
|
273
275
|
|
274
|
-
def defined_attributes=defined_attributes
|
275
|
-
@defined_attributes = defined_attributes
|
276
|
-
end
|
277
|
-
|
278
276
|
def attribute_defined?(attr)
|
279
277
|
defined_attributes.has_key?(attr.to_sym)
|
280
278
|
end
|
@@ -300,10 +298,6 @@ module SimpleModel
|
|
300
298
|
@default_attribute_settings ||= DEFAULT_ATTRIBUTE_SETTINGS
|
301
299
|
end
|
302
300
|
|
303
|
-
def default_attribute_settings=default_attribute_settings
|
304
|
-
@default_attribute_settings = default_attribute_settings
|
305
|
-
end
|
306
|
-
|
307
301
|
# We want to re-run define_attribute_methods since attributes are not all defined
|
308
302
|
# at once, so we must set @attribute_methods_generated to nil to allow the
|
309
303
|
# re-run to occur ONLY IN RAILS 3.0.
|
@@ -335,6 +329,7 @@ module SimpleModel
|
|
335
329
|
define_method(attr) do
|
336
330
|
get_attribute(attr,options)
|
337
331
|
end
|
332
|
+
|
338
333
|
define_method("#{attr}?") do
|
339
334
|
get_attribute?(attr)
|
340
335
|
end
|
@@ -375,13 +370,6 @@ module SimpleModel
|
|
375
370
|
|
376
371
|
# A hook to perform actions on the pending attributes or the object before
|
377
372
|
# the pending attributes have been initialized.
|
378
|
-
# Expects an lambda that accept the object, the pending attributes hash and
|
379
|
-
# should return a hash to be set
|
380
|
-
# EX: lambda {|obj,attrs| attrs.select{|k,v| !v.blank?}}
|
381
|
-
def before_initialize
|
382
|
-
@before_initialize
|
383
|
-
end
|
384
|
-
|
385
373
|
# Expects an lambda that accept the object, the pending attributes hash and
|
386
374
|
# should return a hash to be set
|
387
375
|
# EX: lambda {|obj,attrs| attrs.select{|k,v| !v.blank?}}
|
@@ -391,12 +379,6 @@ module SimpleModel
|
|
391
379
|
end
|
392
380
|
|
393
381
|
# A hook to perform actions after all attributes have been initialized
|
394
|
-
# Expects an lambda that accept the object and the pending attributes hash
|
395
|
-
# EX: lambda {|obj| puts "initialized"}
|
396
|
-
def after_initialize
|
397
|
-
@after_initialize
|
398
|
-
end
|
399
|
-
|
400
382
|
# Expects an lambda that accept the object and the pending attributes hash
|
401
383
|
# EX: lambda {|obj| puts "initialized"}
|
402
384
|
def after_initialize=after_initialize
|
data/lib/simple_model/base.rb
CHANGED
@@ -83,6 +83,11 @@ module SimpleModel
|
|
83
83
|
|
84
84
|
class << self
|
85
85
|
|
86
|
+
def after_any(meth=nil)
|
87
|
+
@after_any = meth if meth
|
88
|
+
@after_any
|
89
|
+
end
|
90
|
+
|
86
91
|
# Defines the model action's instance methods and applied defaults. For every
|
87
92
|
# action defined, we also define that actions ! method which raises exceptions
|
88
93
|
# when the action fails.
|
@@ -91,11 +96,14 @@ module SimpleModel
|
|
91
96
|
actions = [action,"#{action}!".to_sym]
|
92
97
|
actions.each do |a|
|
93
98
|
define_method(a) do |opts = {}|
|
99
|
+
rslt = nil
|
94
100
|
options = default_options.merge(opts)
|
95
101
|
options[:raise_exception] = a.to_s.match(/\!$/)
|
96
102
|
run_callbacks(action) do
|
97
|
-
run_model_action(methods,options)
|
103
|
+
rslt = run_model_action(methods,options)
|
98
104
|
end
|
105
|
+
run_after_any
|
106
|
+
rslt
|
99
107
|
end
|
100
108
|
end
|
101
109
|
end
|
@@ -126,6 +134,10 @@ module SimpleModel
|
|
126
134
|
|
127
135
|
private
|
128
136
|
|
137
|
+
def run_after_any
|
138
|
+
self.send(self.class.after_any) if self.class.after_any
|
139
|
+
end
|
140
|
+
|
129
141
|
# Skeleton for action instance methods
|
130
142
|
def run_model_action(methods,options)
|
131
143
|
completed = true
|
data/lib/simple_model/version.rb
CHANGED
@@ -94,12 +94,17 @@ describe SimpleModel::Base do
|
|
94
94
|
save :my_save_method
|
95
95
|
before_validation :set_foo
|
96
96
|
after_validation :set_bar
|
97
|
-
attr_accessor :foo,:bar
|
97
|
+
attr_accessor :foo,:bar, :log
|
98
98
|
validates :foo, :presence => true
|
99
|
-
|
99
|
+
after_any :log_stuff
|
100
100
|
|
101
101
|
private
|
102
102
|
|
103
|
+
def log_stuff
|
104
|
+
@log = "stuff"
|
105
|
+
end
|
106
|
+
|
107
|
+
|
103
108
|
def my_save_method
|
104
109
|
true
|
105
110
|
end
|
@@ -125,10 +130,15 @@ describe SimpleModel::Base do
|
|
125
130
|
|
126
131
|
it "should implement ActiveModel::Callbacks" do
|
127
132
|
base_test.save
|
128
|
-
|
129
133
|
expect(base_test.foo).to eql('foo')
|
130
134
|
expect(base_test.bar).to eql('bar')
|
131
135
|
end
|
136
|
+
|
137
|
+
it "should run after any" do
|
138
|
+
allow(base_test).to receive(:foo).and_return(nil)
|
139
|
+
expect(base_test.save).to eql(false)
|
140
|
+
expect(base_test.log).to eql("stuff")
|
141
|
+
end
|
132
142
|
end
|
133
143
|
|
134
144
|
|
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.4.
|
4
|
+
version: 1.4.2
|
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: 2015-
|
11
|
+
date: 2015-09-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|