ohm-contrib 0.0.37 → 0.0.38
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ohm/contrib.rb +1 -1
- data/lib/ohm/contrib/typecast.rb +13 -14
- data/test/typecast_existing_attribute_test.rb +22 -0
- metadata +4 -3
data/lib/ohm/contrib.rb
CHANGED
data/lib/ohm/contrib/typecast.rb
CHANGED
@@ -221,8 +221,8 @@ module Ohm
|
|
221
221
|
end
|
222
222
|
end
|
223
223
|
|
224
|
-
# Provides unobtrusive, non-explosive typecasting.Instead of exploding on
|
225
|
-
# of an invalid value, this module takes the approach of just taking in
|
224
|
+
# Provides unobtrusive, non-explosive typecasting. Instead of exploding on
|
225
|
+
# set of an invalid value, this module takes the approach of just taking in
|
226
226
|
# parameters and letting you do validation yourself. The only thing this
|
227
227
|
# module does for you is the boilerplate casting you might need to do.
|
228
228
|
#
|
@@ -261,7 +261,7 @@ module Ohm
|
|
261
261
|
#
|
262
262
|
# item = Item.create(:price => "299", :posted => Time.now.utc)
|
263
263
|
# item = Item[item.id]
|
264
|
-
# item.price.class ==
|
264
|
+
# item.price.class == BigDecimal
|
265
265
|
# # => true
|
266
266
|
#
|
267
267
|
# item.price.to_s == "299"
|
@@ -313,17 +313,15 @@ module Ohm
|
|
313
313
|
# @return [Array] the array of attributes already defined.
|
314
314
|
# @return [nil] if the attribute is already defined.
|
315
315
|
def attribute(name, type = Ohm::Types::String, klass = Ohm::Types[type])
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
write_local(name, klass[read_local(name)])
|
326
|
-
end
|
316
|
+
# Primitive types maintain a reference to the original object
|
317
|
+
# stored in @_attributes[att]. Hence mutation works for the
|
318
|
+
# Primitive case. For cases like Hash, Array where the value
|
319
|
+
# is `JSON.parse`d, we need to set the actual Ohm::Types::Hash
|
320
|
+
# (or similar) to @_attributes[att] for mutation to work.
|
321
|
+
if klass.superclass == Ohm::Types::Primitive
|
322
|
+
define_method(name) { klass[read_local(name)] }
|
323
|
+
else
|
324
|
+
define_method(name) { write_local(name, klass[read_local(name)]) }
|
327
325
|
end
|
328
326
|
|
329
327
|
define_method(:"#{name}=") do |value|
|
@@ -332,6 +330,7 @@ module Ohm
|
|
332
330
|
|
333
331
|
attributes << name unless attributes.include?(name)
|
334
332
|
end
|
333
|
+
alias :typecast :attribute
|
335
334
|
|
336
335
|
private
|
337
336
|
def const_missing(name)
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require File.expand_path("./helper", File.dirname(__FILE__))
|
4
|
+
|
5
|
+
class Post < Ohm::Model
|
6
|
+
include Ohm::Timestamping
|
7
|
+
include Ohm::Typecast
|
8
|
+
|
9
|
+
typecast :created_at, Time
|
10
|
+
typecast :updated_at, Time
|
11
|
+
end
|
12
|
+
|
13
|
+
test "created_at and updated_at are typecasted" do
|
14
|
+
post = Post.create
|
15
|
+
|
16
|
+
assert post.created_at.respond_to?(:strftime)
|
17
|
+
assert post.updated_at.respond_to?(:strftime)
|
18
|
+
|
19
|
+
assert NOW.strftime("%Y-%m-%d") == post.created_at.strftime("%Y-%m-%d")
|
20
|
+
assert NOW.strftime("%Y-%m-%d") == post.updated_at.strftime("%Y-%m-%d")
|
21
|
+
end
|
22
|
+
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 38
|
9
|
+
version: 0.0.38
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Cyril David
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-10-
|
17
|
+
date: 2010-10-06 00:00:00 +08:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -126,6 +126,7 @@ files:
|
|
126
126
|
- test/typecast_boolean_test.rb
|
127
127
|
- test/typecast_date_test.rb
|
128
128
|
- test/typecast_decimal_test.rb
|
129
|
+
- test/typecast_existing_attribute_test.rb
|
129
130
|
- test/typecast_float_test.rb
|
130
131
|
- test/typecast_hash_test.rb
|
131
132
|
- test/typecast_integer_test.rb
|