simple_model 1.2.8 → 1.2.9
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/gemfiles/3.0.gemfile +1 -1
- data/gemfiles/3.1.gemfile +1 -1
- data/gemfiles/3.2.gemfile +1 -1
- data/lib/simple_model/attributes.rb +71 -75
- data/lib/simple_model/error_helpers.rb +1 -0
- data/lib/simple_model/version.rb +1 -1
- data/spec/attributes_spec.rb +2 -2
- data/spec/error_helpers_spec.rb +15 -0
- data/spec/simple_model_spec.rb +1 -1
- metadata +4 -2
data/Gemfile
CHANGED
data/gemfiles/3.0.gemfile
CHANGED
data/gemfiles/3.1.gemfile
CHANGED
data/gemfiles/3.2.gemfile
CHANGED
@@ -2,32 +2,32 @@ module SimpleModel
|
|
2
2
|
module Attributes
|
3
3
|
include ExtendCore
|
4
4
|
extend ActiveSupport::Concern
|
5
|
-
include ActiveModel::AttributeMethods
|
6
|
-
|
7
|
-
def initialize(*attrs)
|
8
|
-
attrs = attrs.extract_options!
|
5
|
+
include ActiveModel::AttributeMethods
|
6
|
+
|
7
|
+
def initialize(*attrs)
|
8
|
+
attrs = attrs.extract_options!
|
9
9
|
attrs = attributes_with_for_init(attrs)
|
10
10
|
attrs = self.class.before_initialize.call(self,attrs) if self.class.before_initialize
|
11
11
|
set(attrs)
|
12
12
|
self.class.after_initialize.call(self) if self.class.after_initialize
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
# Returns true if attribute has been initialized
|
16
16
|
def initialized?(attr)
|
17
17
|
attributes.key?(attr.to_sym)
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
def attributes
|
21
21
|
@attributes ||= HashWithIndifferentAccess.new
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
def attributes=attrs
|
25
25
|
@attributes = attrs
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
def get(attr)
|
29
29
|
self.send(attr)
|
30
|
-
end
|
30
|
+
end
|
31
31
|
alias :read :get
|
32
32
|
|
33
33
|
# Accepts a hash where the keys are methods and the values are values to be set.
|
@@ -38,14 +38,14 @@ module SimpleModel
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
alias :set_attributes :set
|
41
|
-
|
41
|
+
|
42
42
|
private
|
43
|
-
|
43
|
+
|
44
44
|
def fetch_default_value(arg)
|
45
45
|
return self.send(arg) if (arg.is_a?(Symbol) && self.respond_to?(arg))
|
46
46
|
arg
|
47
47
|
end
|
48
|
-
|
48
|
+
|
49
49
|
# Returns attribute that have defaults in a hash: {:attrbute => "default value"}
|
50
50
|
# Checks for alias attributes to ensure they are not overwritten
|
51
51
|
def attributes_with_for_init(attrs)
|
@@ -57,13 +57,13 @@ module SimpleModel
|
|
57
57
|
end
|
58
58
|
d
|
59
59
|
end
|
60
|
-
|
60
|
+
|
61
61
|
def allow_set_default?(d,k,v)
|
62
62
|
(v[:default] && v[:initialize] && (d[k].blank? && (self.class.alias_attributes[k].blank? || d.key?(self.class.alias_attributes[k]) && d[self.class.alias_attributes[k]].blank?)))
|
63
63
|
end
|
64
|
-
|
64
|
+
|
65
65
|
private
|
66
|
-
|
66
|
+
|
67
67
|
def allow_attribute_action?(obj,val,options)
|
68
68
|
return true if (options[:if].blank? && options[:unless].blank?)
|
69
69
|
b = true
|
@@ -85,18 +85,18 @@ module SimpleModel
|
|
85
85
|
b = (b && !options[:unless].call(obj,val)) if options[:unless].is_a?(Proc)
|
86
86
|
b
|
87
87
|
end
|
88
|
-
|
88
|
+
|
89
89
|
# Rails 3.2 + required when searching for attributes in from inherited classes/cmodles
|
90
90
|
def attribute(name)
|
91
91
|
attributes[name.to_sym]
|
92
92
|
end
|
93
|
-
|
94
|
-
module ClassMethods
|
93
|
+
|
94
|
+
module ClassMethods
|
95
95
|
# Creates a new instance where the attributes store is set to object
|
96
96
|
# provided, which allows one to pass a session store hash or any other
|
97
97
|
# hash-like object to be used for persistance. Typically used for modeling
|
98
98
|
# session stores for authorization or shopping carts
|
99
|
-
# EX:
|
99
|
+
# EX:
|
100
100
|
# class ApplicationController < ActionController::Base
|
101
101
|
# def session_user
|
102
102
|
# session[:user] ||= {}
|
@@ -111,23 +111,23 @@ module SimpleModel
|
|
111
111
|
new.set(new.send(:attributes_with_for_init,session_hash))
|
112
112
|
new
|
113
113
|
end
|
114
|
-
|
114
|
+
|
115
115
|
def alias_attributes
|
116
116
|
@alias_attributes ||= {}.with_indifferent_access
|
117
117
|
end
|
118
|
-
|
118
|
+
|
119
119
|
def alias_attributes=alias_attributes
|
120
120
|
@alias_attributes = alias_attributes
|
121
121
|
end
|
122
|
-
|
122
|
+
|
123
123
|
def defined_attributes
|
124
124
|
@defined_attributes ||= {}.with_indifferent_access
|
125
125
|
end
|
126
|
-
|
126
|
+
|
127
127
|
def defined_attributes=defined_attributes
|
128
128
|
@defined_attributes = defined_attributes
|
129
129
|
end
|
130
|
-
|
130
|
+
|
131
131
|
# The default settings for a SimpeModel class
|
132
132
|
# Options:
|
133
133
|
# * :on_set - accepts a lambda that is run when an attribute is set
|
@@ -138,17 +138,17 @@ module SimpleModel
|
|
138
138
|
# * :allow_blank - when set to false, if an attributes value is blank attempts to set the default value, defaults to true
|
139
139
|
def default_attribute_settings
|
140
140
|
@default_attribute_settings ||= {:attributes_method => :attributes,
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
141
|
+
:on_set => lambda {|obj,attr| attr},
|
142
|
+
:on_get => lambda {|obj,attr| attr},
|
143
|
+
:allow_blank => true,
|
144
|
+
:initialize => true
|
145
|
+
}
|
146
146
|
end
|
147
|
-
|
147
|
+
|
148
148
|
def default_attribute_settings=default_attribute_settings
|
149
149
|
@default_attribute_settings = default_attribute_settings
|
150
150
|
end
|
151
|
-
|
151
|
+
|
152
152
|
# We want to re-run define_attribute_methods since attributes are not all defined
|
153
153
|
# at once, so we must set @attribute_methods_generated to nil to allow the
|
154
154
|
# re-run to occur ONLY IN RAILS 3.0.
|
@@ -157,17 +157,17 @@ module SimpleModel
|
|
157
157
|
@attribute_methods_generated = nil #if (ActiveModel::VERSION::MAJOR == 3 && ActiveModel::VERSION::MINOR == 0)
|
158
158
|
define_attribute_methods(self.defined_attributes.keys)
|
159
159
|
end
|
160
|
-
|
160
|
+
|
161
161
|
# builds the setter and getter methods
|
162
162
|
def create_attribute_methods(attributes,options)
|
163
|
-
unless attributes.blank?
|
163
|
+
unless attributes.blank?
|
164
164
|
attributes.each do |attr|
|
165
165
|
define_reader_with_options(attr,options)
|
166
166
|
define_setter_with_options(attr,options)
|
167
167
|
end
|
168
168
|
end
|
169
169
|
end
|
170
|
-
|
170
|
+
|
171
171
|
def define_reader_with_options(attr,options)
|
172
172
|
add_defined_attribute(attr,options)
|
173
173
|
options = default_attribute_settings.merge(options) if options[:on_get].blank?
|
@@ -181,43 +181,43 @@ module SimpleModel
|
|
181
181
|
define_method("#{attr.to_s}?") do
|
182
182
|
val = self.send(attr)
|
183
183
|
if val.respond_to?(:to_b)
|
184
|
-
val = val.to_b
|
184
|
+
val = val.to_b
|
185
185
|
else
|
186
186
|
val = !val.blank? if val.respond_to?(:blank?)
|
187
187
|
end
|
188
188
|
val
|
189
189
|
end
|
190
190
|
end
|
191
|
-
|
191
|
+
|
192
192
|
# Creates setter methods for the provided attributes
|
193
|
-
# On set, it will mark the attribute as changed if the attributes has been
|
193
|
+
# On set, it will mark the attribute as changed if the attributes has been
|
194
194
|
# initialized.
|
195
195
|
def define_setter_with_options(attr,options)
|
196
196
|
add_defined_attribute(attr,options)
|
197
|
-
options = default_attribute_settings.merge(options) if (options[:on_set].blank? || options[:after_set].blank?)
|
197
|
+
options = default_attribute_settings.merge(options) if (options[:on_set].blank? || options[:after_set].blank?)
|
198
198
|
define_method("#{attr.to_s}=") do |val|
|
199
199
|
if allow_attribute_action?(self,val,options)
|
200
|
-
val = fetch_default_value(options[:default]) if (!options[:allow_blank] && options.key?(:default) && val.blank?)
|
200
|
+
val = fetch_default_value(options[:default]) if (!options[:allow_blank] && options.key?(:default) && val.blank?)
|
201
201
|
val = options[:on_set].call(self,val) unless (val.blank? && !options[:allow_blank] )
|
202
202
|
will_change = "#{attr}_will_change!".to_sym
|
203
|
-
self.send(will_change) if (initialized?(attr) && val != self.attributes[attr])
|
203
|
+
self.send(will_change) if (initialized?(attr) && val != self.attributes[attr])
|
204
204
|
self.attributes[attr] = val
|
205
|
-
options[:after_set].call(self,val) if options[:after_set]
|
205
|
+
options[:after_set].call(self,val) if options[:after_set]
|
206
206
|
end
|
207
207
|
end
|
208
208
|
end
|
209
|
-
|
209
|
+
|
210
210
|
AVAILABLE_ATTRIBUTE_METHODS = {
|
211
211
|
:has_attribute => {:alias => :has_attributes},
|
212
212
|
:has_boolean => {:cast_to => :to_b, :alias => :has_booleans},
|
213
213
|
:has_currency => {:cast_to => :to_d, :alias => :has_currencies},
|
214
|
-
:has_date => {:cast_to => :to_date, :alias => :has_dates} ,
|
215
|
-
:has_decimal => {:cast_to => :to_d, :alias => :has_decimals},
|
216
|
-
:has_float => {:cast_to => :to_f, :alias => :has_floats},
|
214
|
+
:has_date => {:cast_to => :to_date, :alias => :has_dates} ,
|
215
|
+
:has_decimal => {:cast_to => :to_d, :alias => :has_decimals},
|
216
|
+
:has_float => {:cast_to => :to_f, :alias => :has_floats},
|
217
217
|
:has_int => {:cast_to => :to_i, :alias => :has_ints},
|
218
|
-
:has_time => {:cast_to => :to_time, :alias => :has_times}
|
218
|
+
:has_time => {:cast_to => :to_time, :alias => :has_times}
|
219
219
|
}
|
220
|
-
|
220
|
+
|
221
221
|
AVAILABLE_ATTRIBUTE_METHODS.each do |method,method_options|
|
222
222
|
define_method(method) do |*attributes|
|
223
223
|
options = default_attribute_settings.merge(attributes.extract_options!)
|
@@ -226,19 +226,19 @@ module SimpleModel
|
|
226
226
|
end
|
227
227
|
module_eval("alias #{method_options[:alias]} #{method}")
|
228
228
|
end
|
229
|
-
|
229
|
+
|
230
230
|
# Creates alias setter and getter for the supplied attribute using the supplied alias
|
231
231
|
# See spec for example.
|
232
232
|
def alias_attribute(new_alias,attribute)
|
233
233
|
alias_attributes[attribute] = new_alias
|
234
|
-
define_method(new_alias) do
|
234
|
+
define_method(new_alias) do
|
235
235
|
self.send(attribute)
|
236
236
|
end
|
237
237
|
define_method("#{new_alias.to_s}=") do |*args, &block|
|
238
238
|
self.send("#{attribute.to_s}=",*args, &block)
|
239
239
|
end
|
240
240
|
end
|
241
|
-
|
241
|
+
|
242
242
|
# A hook to perform actions on the pending attributes or the object before
|
243
243
|
# the pending attributes have been initialized.
|
244
244
|
# Expects an lambda that accept the object, the pending attributes hash and
|
@@ -247,66 +247,62 @@ module SimpleModel
|
|
247
247
|
def before_initialize
|
248
248
|
@before_initialize
|
249
249
|
end
|
250
|
-
|
250
|
+
|
251
251
|
# Expects an lambda that accept the object, the pending attributes hash and
|
252
252
|
# should return a hash to be set
|
253
|
-
# EX: lambda {|obj,attrs| attrs.select{|k,v| !v.blank?}}
|
253
|
+
# EX: lambda {|obj,attrs| attrs.select{|k,v| !v.blank?}}
|
254
254
|
def before_initialize=before_initialize
|
255
255
|
raise TypeError "before_initialize must be a lambda that accepts the attirbutes to be initialize" unless before_initialize.is_a?(Proc)
|
256
256
|
@before_initialize = before_initialize
|
257
257
|
end
|
258
|
-
|
258
|
+
|
259
259
|
# A hook to perform actions after all attributes have been initialized
|
260
260
|
# Expects an lambda that accept the object and the pending attributes hash
|
261
261
|
# EX: lambda{|obj| puts "initialized"}
|
262
262
|
def after_initialize
|
263
263
|
@after_initialize
|
264
264
|
end
|
265
|
-
|
265
|
+
|
266
266
|
# Expects an lambda that accept the object and the pending attributes hash
|
267
267
|
# EX: lambda{|obj| puts "initialized"}
|
268
268
|
def after_initialize=after_initialize
|
269
269
|
raise TypeError "after_initalize must be a Proc" unless after_initialize.is_a?(Proc)
|
270
270
|
@after_initialize = after_initialize
|
271
271
|
end
|
272
|
-
|
273
|
-
|
274
|
-
|
272
|
+
|
273
|
+
|
274
|
+
|
275
275
|
# Must inherit super's defined_attributes and alias_attributes
|
276
276
|
# Rails 3.0 does some weird stuff with ActiveModel::Dirty so we need a
|
277
|
-
# hack to keep things working when a class
|
277
|
+
# hack to keep things working when a class inherits from a super that
|
278
278
|
# has ActiveModel::Dirty included
|
279
279
|
def inherited(base)
|
280
|
+
super
|
280
281
|
# Rails 3.0 Hack
|
281
|
-
if (ActiveModel::VERSION::MAJOR == 3 && ActiveModel::VERSION::MINOR == 0)
|
282
|
+
if (ActiveModel::VERSION::MAJOR == 3 && ActiveModel::VERSION::MINOR == 0)
|
282
283
|
base.attribute_method_suffix '_changed?', '_change', '_will_change!', '_was'
|
283
284
|
base.attribute_method_affix :prefix => 'reset_', :suffix => '!'
|
284
285
|
end
|
285
|
-
base.
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
base.create_attribute_methods([attr],options)
|
290
|
-
end
|
286
|
+
base.defined_attributes = self.defined_attributes.merge(base.defined_attributes)
|
287
|
+
base.alias_attributes = self.alias_attributes.merge(base.alias_attributes)
|
288
|
+
base.defined_attributes.each do |attr,options|
|
289
|
+
base.create_attribute_methods([attr],options) #unless base.instance_methods.include?(attr.to_sym)
|
291
290
|
end
|
292
|
-
|
293
|
-
base.alias_attributes = self.alias_attributes.merge(base.alias_attributes )
|
294
|
-
super
|
295
291
|
end
|
296
292
|
end
|
297
|
-
|
298
|
-
# Rails 3.0 does some weird stuff with ActiveModel::Dirty so we need a
|
299
|
-
# hack to keep things working when a class includes a module that has
|
293
|
+
|
294
|
+
# Rails 3.0 does some weird stuff with ActiveModel::Dirty so we need a
|
295
|
+
# hack to keep things working when a class includes a module that has
|
300
296
|
# ActiveModel::Dirty included
|
301
297
|
def self.included(base)
|
302
|
-
base.extend(Attributes::ClassMethods)
|
303
|
-
base.send(:include, ActiveModel::Dirty)
|
298
|
+
base.extend(Attributes::ClassMethods)
|
299
|
+
base.send(:include, ActiveModel::Dirty)
|
304
300
|
base.send(:include, ActiveModel::Validations)
|
305
301
|
base.send(:include, ActiveModel::Conversion)
|
306
302
|
base.extend ActiveModel::Naming
|
307
303
|
base.extend ActiveModel::Callbacks
|
308
304
|
base.send(:include, ActiveModel::Validations::Callbacks)
|
309
|
-
|
305
|
+
|
310
306
|
# Rails 3.0 Hack
|
311
307
|
if (ActiveModel::VERSION::MAJOR == 3 && ActiveModel::VERSION::MINOR == 0)
|
312
308
|
base.attribute_method_suffix '_changed?', '_change', '_will_change!', '_was'
|
@@ -314,4 +310,4 @@ module SimpleModel
|
|
314
310
|
end
|
315
311
|
end
|
316
312
|
end
|
317
|
-
end
|
313
|
+
end
|
data/lib/simple_model/version.rb
CHANGED
data/spec/attributes_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper.rb'
|
2
2
|
|
3
3
|
describe SimpleModel::Attributes do
|
4
4
|
before(:all) do
|
@@ -246,7 +246,7 @@ describe SimpleModel::Attributes do
|
|
246
246
|
end
|
247
247
|
end
|
248
248
|
it "should merge defined attributes when class are inhereted" do
|
249
|
-
NewerBase.defined_attributes[:bar].
|
249
|
+
NewerBase.defined_attributes[:bar].should_not be_nil
|
250
250
|
n = NewerBase.new
|
251
251
|
n.respond_to?(:bar_will_change!).should be_true
|
252
252
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper.rb'
|
2
|
+
|
3
|
+
class FooErrorClass < SimpleModel::Base
|
4
|
+
end
|
5
|
+
|
6
|
+
describe SimpleModel::ErrorHelpers do
|
7
|
+
describe '#errors_for_flash' do
|
8
|
+
it "should not raise error" do
|
9
|
+
f = FooErrorClass.new
|
10
|
+
f.errors.add(:foo, "something not great")
|
11
|
+
f.errors.add(:bar, "doh")
|
12
|
+
lambda {f.errors_for_flash}.should_not raise_error
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/spec/simple_model_spec.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-04-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -104,6 +104,7 @@ files:
|
|
104
104
|
- lib/simple_model/version.rb
|
105
105
|
- simple_model.gemspec
|
106
106
|
- spec/attributes_spec.rb
|
107
|
+
- spec/error_helpers_spec.rb
|
107
108
|
- spec/extend_core_spec.rb
|
108
109
|
- spec/simple_model_spec.rb
|
109
110
|
- spec/spec_helper.rb
|
@@ -133,6 +134,7 @@ specification_version: 3
|
|
133
134
|
summary: Simpifies building tableless models or models backed by webservices
|
134
135
|
test_files:
|
135
136
|
- spec/attributes_spec.rb
|
137
|
+
- spec/error_helpers_spec.rb
|
136
138
|
- spec/extend_core_spec.rb
|
137
139
|
- spec/simple_model_spec.rb
|
138
140
|
- spec/spec_helper.rb
|