attribution 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/attribution.gemspec +1 -1
- data/lib/attribution/version.rb +1 -1
- data/lib/attribution.rb +8 -5
- data/test/attribution_test.rb +23 -0
- metadata +1 -1
data/attribution.gemspec
CHANGED
data/lib/attribution/version.rb
CHANGED
data/lib/attribution.rb
CHANGED
@@ -54,7 +54,7 @@ module Attribution
|
|
54
54
|
def string(attr, metadata={})
|
55
55
|
add_attribute(attr, :string, metadata)
|
56
56
|
define_method("#{attr}=") do |arg|
|
57
|
-
instance_variable_set("@#{attr}", arg.to_s)
|
57
|
+
instance_variable_set("@#{attr}", arg.nil? ? nil : arg.to_s)
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
@@ -64,6 +64,7 @@ module Attribution
|
|
64
64
|
v = case arg
|
65
65
|
when String then BOOLEAN_TRUE_STRINGS.include?(arg.downcase)
|
66
66
|
when Numeric then arg == 1
|
67
|
+
when nil then nil
|
67
68
|
else !!arg
|
68
69
|
end
|
69
70
|
instance_variable_set("@#{attr}", v)
|
@@ -74,21 +75,21 @@ module Attribution
|
|
74
75
|
def integer(attr, metadata={})
|
75
76
|
add_attribute(attr, :integer, metadata)
|
76
77
|
define_method("#{attr}=") do |arg|
|
77
|
-
instance_variable_set("@#{attr}", arg.to_i)
|
78
|
+
instance_variable_set("@#{attr}", arg.nil? ? nil : arg.to_i)
|
78
79
|
end
|
79
80
|
end
|
80
81
|
|
81
82
|
def float(attr, metadata={})
|
82
83
|
add_attribute(attr, :float, metadata)
|
83
84
|
define_method("#{attr}=") do |arg|
|
84
|
-
instance_variable_set("@#{attr}", arg.to_f)
|
85
|
+
instance_variable_set("@#{attr}", arg.nil? ? nil : arg.to_f)
|
85
86
|
end
|
86
87
|
end
|
87
88
|
|
88
89
|
def decimal(attr, metadata={})
|
89
90
|
add_attribute(attr, :decimal, metadata)
|
90
91
|
define_method("#{attr}=") do |arg|
|
91
|
-
instance_variable_set("@#{attr}", BigDecimal.new(arg.to_s))
|
92
|
+
instance_variable_set("@#{attr}", arg.nil? ? nil : BigDecimal.new(arg.to_s))
|
92
93
|
end
|
93
94
|
end
|
94
95
|
|
@@ -100,6 +101,7 @@ module Attribution
|
|
100
101
|
when Time, DateTime then arg.to_date
|
101
102
|
when String then Date.parse(arg)
|
102
103
|
when Hash then Date.new(*arg.slice(:year, :month, :day).values.map(&:to_i))
|
104
|
+
when nil then nil
|
103
105
|
else raise ArgumentError.new("can't convert #{arg.class} to Date")
|
104
106
|
end
|
105
107
|
instance_variable_set("@#{attr}", v)
|
@@ -114,6 +116,7 @@ module Attribution
|
|
114
116
|
when Time then arg
|
115
117
|
when String then Time.parse(arg)
|
116
118
|
when Hash then Time.new(*arg.slice(:year, :month, :day, :hour, :min, :sec, :utc_offset).values.map(&:to_i))
|
119
|
+
when nil then nil
|
117
120
|
else raise ArgumentError.new("can't convert #{arg.class} to Time")
|
118
121
|
end
|
119
122
|
instance_variable_set("@#{attr}", v)
|
@@ -123,7 +126,7 @@ module Attribution
|
|
123
126
|
def time_zone(attr, metadata={})
|
124
127
|
add_attribute(attr, :time_zone, metadata)
|
125
128
|
define_method("#{attr}=") do |arg|
|
126
|
-
instance_variable_set("@#{attr}", ActiveSupport::TimeZone[arg.to_s])
|
129
|
+
instance_variable_set("@#{attr}", arg.nil? ? nil : ActiveSupport::TimeZone[arg.to_s])
|
127
130
|
end
|
128
131
|
end
|
129
132
|
|
data/test/attribution_test.rb
CHANGED
@@ -124,4 +124,27 @@ class AttributionTest < Test::Unit::TestCase
|
|
124
124
|
book = Book.new(:created_at => { :year => '2013', :month => '03', :day => '17', :hour => '07', :min => '30', :sec => '11', :utc_offset => '3600' })
|
125
125
|
assert_equal Time.parse('2013-03-17 07:30:11 +01:00'), book.created_at
|
126
126
|
end
|
127
|
+
|
128
|
+
def test_nil
|
129
|
+
book = Book.new(
|
130
|
+
:id => nil,
|
131
|
+
:title => nil,
|
132
|
+
:price => nil,
|
133
|
+
:published_on => nil,
|
134
|
+
:ebook_available => nil,
|
135
|
+
:used => nil,
|
136
|
+
:shipping_weight => nil,
|
137
|
+
:created_at => nil,
|
138
|
+
:time_zone => nil,
|
139
|
+
)
|
140
|
+
assert_equal nil, book.id
|
141
|
+
assert_equal nil, book.title
|
142
|
+
assert_equal nil, book.price
|
143
|
+
assert_equal nil, book.published_on
|
144
|
+
assert_equal nil, book.ebook_available
|
145
|
+
assert_equal nil, book.used
|
146
|
+
assert_equal nil, book.shipping_weight
|
147
|
+
assert_equal nil, book.created_at
|
148
|
+
assert_equal nil, book.time_zone
|
149
|
+
end
|
127
150
|
end
|