Created
January 11, 2015 02:10
-
-
Save burlesona/a0bd26437f91f72bf0fa to your computer and use it in GitHub Desktop.
Hash <-> Date
This file contains hidden or 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
class Hash | |
def to_date | |
Date.new fetch(:year), fetch(:month), fetch(:day) | |
end | |
end | |
class Date | |
def to_hash | |
{year: year, month: month, day: day} | |
end | |
end | |
require 'minitest/autorun' | |
require 'minitest/spec' | |
describe "core extensions" do | |
describe "hash" do | |
it "should have to_date" do | |
res = {year: 2015, month: 12, day:25}.to_date | |
assert res.is_a?(Date) | |
assert_equal 2015, res.year | |
assert_equal 12, res.month | |
assert_equal 25, res.day | |
end | |
end | |
describe "date" do | |
it "should have to_hash" do | |
res = Date.new(2015,12,25).to_hash | |
assert res.is_a?(Hash) | |
assert_equal 2015, res[:year] | |
assert_equal 12, res[:month] | |
assert_equal 25, res[:day] | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment