Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting "Date" and "Time" type #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/json_mapper/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def typecast(value)
end
elsif self.type == Boolean then return %w(true t 1).include?(value.to_s.downcase)
elsif self.type == DateTime then return Date.parse(value.to_s)
elsif self.type == Date then return Date.parse(value.to_s)
elsif self.type == Time then return Time.parse(value.to_s)
else

# If our type is a JSONMapper instance, delegate the
Expand Down
4 changes: 3 additions & 1 deletion test/fixtures/simple.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
"money": 125.50,
"title": "Simple JSON title",
"boolean": true,
"datetime": "2010-10-08 17:59:46"
"datetime": "2010-10-08 17:59:46",
"date": "2016-08-22 19:34:03",
"time": "2016-08-23 19:56:47"
}
2 changes: 2 additions & 0 deletions test/json_mapper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def to_s
model.title.should == "Simple JSON title"
model.boolean.should == true
model.datetime.should == Date.parse("2010-10-08 17:59:46")
model.date.should == Date.parse("2016-08-22 19:34:03")
model.time.should == Time.parse("2016-08-23 19:56:47")
end

should "assign value from different sources into an attribute" do
Expand Down
2 changes: 2 additions & 0 deletions test/support/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class SimpleModel
json_attribute :title, String
json_attribute :boolean, Boolean
json_attribute :datetime, DateTime
json_attribute :date, Date
json_attribute :time, Time

end

Expand Down