Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
77 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters