Skip to content

Commit

Permalink
Added DateTypecaster, for #5
Browse files Browse the repository at this point in the history
  • Loading branch information
cgriego committed Jan 18, 2012
1 parent c91c419 commit a4f8e31
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 3 deletions.
1 change: 1 addition & 0 deletions active_attr.gemspec
Expand Up @@ -25,4 +25,5 @@ Gem::Specification.new do |s|
s.add_development_dependency "factory_girl", "~> 2.2"
s.add_development_dependency "rake", "~> 0.9.0"
s.add_development_dependency "rspec", "~> 2.6"
s.add_development_dependency "tzinfo", "~> 0.3.29"
end
8 changes: 5 additions & 3 deletions lib/active_attr/typecasting.rb
@@ -1,3 +1,4 @@
require "active_attr/typecasting/date_typecaster"
require "active_attr/typecasting/float_typecaster"
require "active_attr/typecasting/integer_typecaster"
require "active_attr/typecasting/string_typecaster"
Expand All @@ -12,9 +13,10 @@ module Typecasting

# @private
TYPECASTERS = {
Float => FloatTypecaster,
Integer => IntegerTypecaster,
String => StringTypecaster,
Date => DateTypecaster,
Float => FloatTypecaster,
Integer => IntegerTypecaster,
String => StringTypecaster,
}

# Typecasts a value using a Class
Expand Down
15 changes: 15 additions & 0 deletions lib/active_attr/typecasting/date_typecaster.rb
@@ -0,0 +1,15 @@
require "date"
require "active_support/core_ext/date/conversions"
require "active_support/core_ext/date_time/conversions"
require "active_support/core_ext/string/conversions"
require "active_support/core_ext/time/publicize_conversion_methods"

module ActiveAttr
module Typecasting
class DateTypecaster
def call(value)
value.to_date if value.respond_to? :to_date
end
end
end
end
51 changes: 51 additions & 0 deletions spec/unit/active_attr/typecasting/date_typecaster_spec.rb
@@ -0,0 +1,51 @@
require "spec_helper"
require "active_attr/typecasting/date_typecaster"

module ActiveAttr
module Typecasting
describe DateTypecaster do
describe "#call" do
it "returns the original date for a Date" do
value = Date.today
subject.call(value).should equal value
end

it "casts a UTC Time to a Date representing the date portion" do
subject.call(Time.utc(2012, 1, 1, 0, 0, 0)).should == Date.new(2012, 1, 1)
end

it "casts a local Time to a Date representing the date portion" do
subject.call(Time.local(2012, 1, 1, 0, 0, 0)).should == Date.new(2012, 1, 1)
end

it "casts a UTC DateTime to a Date representing the date portion" do
subject.call(DateTime.new(2012, 1, 1, 0, 0, 0)).should == Date.new(2012, 1, 1)
end

it "casts an offset DateTime to a Date representing the date portion" do
subject.call(DateTime.new(2012, 1, 1, 0, 0, 0, "-12:00")).should == Date.new(2012, 1, 1)
end

it "casts an ISO8601 date String to a Date" do
subject.call("2012-01-01").should == Date.new(2012, 1, 1)
end

it "casts an RFC2616 date and GMT time String to a Date representing the date portion" do
subject.call("Sat, 03 Feb 2001 00:00:00 GMT").should == Date.new(2001, 2, 3)
end

it "casts an RFC3339 date and offset time String to a Date representing the date portion" do
subject.call("2001-02-03T04:05:06+07:00").should == Date.new(2001, 2, 3)
end

it "casts a UTC ActiveSupport::TimeWithZone to a Date representing the date portion" do
subject.call(Time.utc(2012, 1, 1, 0, 0, 0).in_time_zone("UTC")).should == Date.new(2012, 1, 1)
end

it "casts an Alaska ActiveSupport::TimeWithZone to a Date representing the date portion" do
subject.call(Time.utc(2012, 1, 1, 0, 0, 0).in_time_zone("Alaska")).should == Date.new(2011, 12, 31)
end
end
end
end
end
5 changes: 5 additions & 0 deletions spec/unit/active_attr/typecasting_spec.rb
Expand Up @@ -87,6 +87,11 @@ module ActiveAttr
Typecasting::IntegerTypecaster.any_instance.should_receive(:call).with(value)
model.typecast_value(Integer, value)
end

it "calls DateTypecaster when typecasting to Date" do
Typecasting::DateTypecaster.any_instance.should_receive(:call).with(value)
model.typecast_value(Date, value)
end
end
end
end

0 comments on commit a4f8e31

Please sign in to comment.