simple_model 1.2.25 → 1.2.26
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/simple_model/attributes.rb +15 -9
- data/lib/simple_model/version.rb +1 -1
- data/spec/attributes_spec.rb +47 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NDZkZTQzYzExYjM5MWRiMzhmZjViZGRkYjA5NDQ1MjcxY2M1NDE4YQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ODU0YTMzMWM4OTA0NGRlYWVkM2U1YTA2MTJhY2Q4MTQxNzc5YTE1NQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZGM5YTJlY2I2NjU2Y2RkMmQ5MWY2MWJmNmMxNTQyOTdhZWQ1MzYxY2U0NGUy
|
10
|
+
OTQzZWZmN2RkNjg5NWIzMzUzZGM0YzBhYTMxNjk3ZTBhYWRmNjVkZmQwYjcz
|
11
|
+
YzFlY2VhZDE0ODM2YTExZmM0ODZlZDAyZTUzNzQ4OTc3MWQxYjU=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NGViMDY2YzUwYzhlYzdiZWNiMDFkNjg4NmY2MmY0NTc3YzMxN2QzN2E2ODgx
|
14
|
+
MDJlODJkM2VlMjkzYmY4MzJhNGQ4MzgyY2E4ZDZiYjcwZWU2Yzk3ZWIyOTFj
|
15
|
+
ZmZlMDgzYWQ5MDExNWUxZDYwMDE5ZGMzZDNiMWM3MzI3Y2EzNDQ=
|
@@ -39,11 +39,15 @@ module SimpleModel
|
|
39
39
|
def set_attribute(attr,val)
|
40
40
|
options = self.class.defined_attributes[attr] || {}
|
41
41
|
if allow_attribute_action?(val,options)
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
42
|
+
allow_blank = options[:allow_blank]
|
43
|
+
default = options[:default]
|
44
|
+
val = fetch_default_value(default) if (!allow_blank && default && val.blank?)
|
45
|
+
unless (val.blank? && !allow_blank)
|
46
|
+
val = options[:on_set].call(self,val) if options.key?(:on_set)
|
47
|
+
send("#{attr}_will_change!") if (initialized?(attr) && val != attributes[attr])
|
48
|
+
attributes[attr] = val
|
49
|
+
options[:after_set].call(self,val) if options[:after_set]
|
50
|
+
end
|
47
51
|
end
|
48
52
|
end
|
49
53
|
|
@@ -136,13 +140,13 @@ module SimpleModel
|
|
136
140
|
|
137
141
|
module ClassMethods
|
138
142
|
DEFAULT_ATTRIBUTE_SETTINGS = {:attributes_method => :attributes,
|
139
|
-
:allow_blank =>
|
143
|
+
:allow_blank => false,
|
140
144
|
:initialize => true
|
141
145
|
}.freeze
|
142
146
|
|
143
147
|
AVAILABLE_ATTRIBUTE_METHODS = {
|
144
|
-
:has_attribute => {:alias => :has_attributes},
|
145
|
-
:has_boolean => {:cast_to => :to_b, :alias => :has_booleans},
|
148
|
+
:has_attribute => {:alias => :has_attributes, :options => {:allow_blank => true}},
|
149
|
+
:has_boolean => {:cast_to => :to_b, :alias => :has_booleans, :options => {:allow_blank => true}},
|
146
150
|
:has_currency => {:cast_to => :to_d, :alias => :has_currencies},
|
147
151
|
:has_date => {:cast_to => :to_date, :alias => :has_dates} ,
|
148
152
|
:has_decimal => {:cast_to => :to_d, :alias => :has_decimals},
|
@@ -153,7 +157,9 @@ module SimpleModel
|
|
153
157
|
|
154
158
|
AVAILABLE_ATTRIBUTE_METHODS.each do |method,method_options|
|
155
159
|
define_method(method) do |*attributes|
|
156
|
-
options =
|
160
|
+
options = attributes.extract_options!
|
161
|
+
options = method_options[:options].merge(options) if method_options[:options]
|
162
|
+
options = default_attribute_settings.merge(options)
|
157
163
|
options[:on_set] = lambda {|obj,val| val.send(method_options[:cast_to]) } if method_options[:cast_to]
|
158
164
|
create_attribute_methods(attributes,options)
|
159
165
|
end
|
data/lib/simple_model/version.rb
CHANGED
data/spec/attributes_spec.rb
CHANGED
@@ -170,6 +170,50 @@ describe SimpleModel::Attributes do
|
|
170
170
|
end
|
171
171
|
end
|
172
172
|
|
173
|
+
context 'number and date/time attributes' do
|
174
|
+
before(:all) do
|
175
|
+
class NumDateTime
|
176
|
+
include SimpleModel::Attributes
|
177
|
+
has_date :my_date
|
178
|
+
has_time :my_time
|
179
|
+
has_int :my_int
|
180
|
+
has_int :blank_int, :allow_blank => true
|
181
|
+
has_decimal :my_decimal
|
182
|
+
has_decimal :blank_decimal, :allow_blank => true
|
183
|
+
has_float :my_float
|
184
|
+
has_attribute :my_attr
|
185
|
+
has_boolean :my_boolean
|
186
|
+
end
|
187
|
+
end
|
188
|
+
context "default options for number attributes" do
|
189
|
+
context "set with blank value" do
|
190
|
+
it "should not initialize" do
|
191
|
+
num_date_time = NumDateTime.new(:my_date => nil, :my_time => "", :my_int => " ", :my_decimal => nil, :my_float => '')
|
192
|
+
expect(num_date_time.initialized?(:my_date)).to eql(false)
|
193
|
+
expect(num_date_time.initialized?(:my_time)).to eql(false)
|
194
|
+
expect(num_date_time.initialized?(:my_int)).to eql(false)
|
195
|
+
expect(num_date_time.initialized?(:my_decimal)).to eql(false)
|
196
|
+
expect(num_date_time.initialized?(:my_float)).to eql(false)
|
197
|
+
end
|
198
|
+
end
|
199
|
+
context "override to allow_blank" do
|
200
|
+
it "should not initialize" do
|
201
|
+
num_date_time = NumDateTime.new(:blank_int => nil, :blank_decimal => "")
|
202
|
+
expect(num_date_time.initialized?(:blank_int)).to eql(true)
|
203
|
+
expect(num_date_time.initialized?(:blank_decimal)).to eql(true)
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
context "set with blank value" do
|
209
|
+
it "should not initialize" do
|
210
|
+
num_date_time = NumDateTime.new(:my_attr => nil, :my_boolean => "")
|
211
|
+
expect(num_date_time.initialized?(:my_attr)).to eql(true)
|
212
|
+
expect(num_date_time.initialized?(:my_boolean)).to eql(true)
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
173
217
|
context 'options with conditional' do
|
174
218
|
before(:all) do
|
175
219
|
class WithConditional
|
@@ -277,9 +321,9 @@ describe SimpleModel::Attributes do
|
|
277
321
|
end
|
278
322
|
|
279
323
|
it "should properly alias attributes from parent class" do
|
280
|
-
|
281
|
-
|
282
|
-
|
324
|
+
nb = NewestBase.new(:some_amount => 1.0)
|
325
|
+
nb.other_amount.should eql(1.0.to_d)
|
326
|
+
nb.amount.should eql(1.0.to_d)
|
283
327
|
end
|
284
328
|
end
|
285
329
|
|
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.26
|
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-12-
|
11
|
+
date: 2014-12-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|