Skip to content

Commit 97673ce

Browse files
authored
Merge pull request #30 from f-mer/feature/date-type
Add Date type
2 parents 42263a0 + 5de74b0 commit 97673ce

File tree

4 files changed

+135
-2
lines changed

4 files changed

+135
-2
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
language: ruby
22
sudo: false
33
cache: bundler
4-
before_install: gem install bundler -v 1.11.2
4+
before_install:
5+
- gem uninstall -v '>= 2' -i $(rvm gemdir)@global -ax bundler || true
6+
- gem install bundler -v '< 2'
57
rvm:
68
- 2.3.1
79
- 2.4.0

lib/shallow_attributes/type.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require 'shallow_attributes/type/integer'
66
require 'shallow_attributes/type/string'
77
require 'shallow_attributes/type/time'
8+
require 'shallow_attributes/type/date'
89

910
module ShallowAttributes
1011
# Namespace for standard type classes
@@ -28,7 +29,8 @@ class InvalidValueError < TypeError
2829
::Float => ShallowAttributes::Type::Float.new,
2930
::Integer => ShallowAttributes::Type::Integer.new,
3031
::String => ShallowAttributes::Type::String.new,
31-
::Time => ShallowAttributes::Type::Time.new
32+
::Time => ShallowAttributes::Type::Time.new,
33+
::Date => ShallowAttributes::Type::Date.new
3234
}.freeze
3335

3436
class << self
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module ShallowAttributes
2+
module Type
3+
# Abstract class for typecast object to Date type.
4+
#
5+
# @abstract
6+
#
7+
# @since 0.1.0
8+
class Date
9+
# Convert value to Date type
10+
#
11+
# @private
12+
#
13+
# @param [Object] value
14+
# @param [Hash] _options
15+
#
16+
# @example Convert string to Date value
17+
# ShallowAttributes::Type::Date.new.coerce('Thu Nov 29 2001')
18+
# # => #<Date: 2001-11-29 ((2452243j,0s,0n),+0s,2299161j)>
19+
#
20+
# @raise [InvalidValueError] if value is not a string
21+
#
22+
# @return [Date]
23+
#
24+
# @since 0.1.0
25+
def coerce(value, options = {})
26+
case value
27+
when ::DateTime, ::Time then value.to_date
28+
when ::Date then value
29+
else
30+
::Date.parse(value.to_s)
31+
end
32+
rescue
33+
if options.fetch(:strict, true)
34+
raise ShallowAttributes::Type::InvalidValueError, %(Invalid value "#{value}" for type "Date")
35+
else
36+
nil
37+
end
38+
end
39+
end
40+
end
41+
end

test/date_type_test.rb

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
require 'test_helper'
2+
3+
describe ShallowAttributes::Type::Date do
4+
let(:type) { ShallowAttributes::Type::Date.new }
5+
6+
describe '#coerce' do
7+
describe 'when value is Date' do
8+
it 'returns date object' do
9+
date = Date.today
10+
type.coerce(date).class.must_equal Date
11+
end
12+
end
13+
14+
describe 'when value is DateTime' do
15+
it 'returns time object' do
16+
time = DateTime.now
17+
type.coerce(time).must_equal time.to_date
18+
end
19+
end
20+
21+
describe 'when value is Time' do
22+
it 'returns time object' do
23+
time = Time.now
24+
type.coerce(time).must_equal time.to_date
25+
end
26+
end
27+
28+
describe 'when value is String' do
29+
it 'returns time object' do
30+
type.coerce('Thu Nov 29 2001').to_s.must_equal '2001-11-29'
31+
end
32+
end
33+
34+
describe 'when value is invalid String' do
35+
it 'returns error' do
36+
err = -> { type.coerce('') }.must_raise ShallowAttributes::Type::InvalidValueError
37+
err.message.must_equal %(Invalid value "" for type "Date")
38+
39+
err = -> { type.coerce('asd') }.must_raise ShallowAttributes::Type::InvalidValueError
40+
err.message.must_equal %(Invalid value "asd" for type "Date")
41+
42+
err = -> { type.coerce('123123') }.must_raise ShallowAttributes::Type::InvalidValueError
43+
err.message.must_equal %(Invalid value "123123" for type "Date")
44+
end
45+
end
46+
47+
describe 'when value is Nil' do
48+
it 'returns InvalidValueError' do
49+
err = -> { type.coerce(nil) }.must_raise ShallowAttributes::Type::InvalidValueError
50+
err.message.must_equal %(Invalid value "" for type "Date")
51+
end
52+
end
53+
54+
describe 'when value is TrueClass' do
55+
it 'returns InvalidValueError' do
56+
err = -> { type.coerce(true) }.must_raise ShallowAttributes::Type::InvalidValueError
57+
err.message.must_equal %(Invalid value "true" for type "Date")
58+
end
59+
end
60+
61+
describe 'when value is FalseClass' do
62+
it 'returns InvalidValueError' do
63+
err = -> { type.coerce(false) }.must_raise ShallowAttributes::Type::InvalidValueError
64+
err.message.must_equal %(Invalid value "false" for type "Date")
65+
end
66+
end
67+
68+
describe 'when value is not allowed' do
69+
it 'returns error' do
70+
err = -> { type.coerce([]) }.must_raise ShallowAttributes::Type::InvalidValueError
71+
err.message.must_equal %(Invalid value "[]" for type "Date")
72+
73+
err = -> { type.coerce({}) }.must_raise ShallowAttributes::Type::InvalidValueError
74+
err.message.must_equal %(Invalid value "{}" for type "Date")
75+
76+
err = -> { type.coerce(:'1') }.must_raise ShallowAttributes::Type::InvalidValueError
77+
err.message.must_equal %(Invalid value "1" for type "Date")
78+
79+
err = -> { type.coerce(Class) }.must_raise ShallowAttributes::Type::InvalidValueError
80+
err.message.must_equal %(Invalid value "Class" for type "Date")
81+
82+
err = -> { type.coerce(Class.new) }.must_raise ShallowAttributes::Type::InvalidValueError
83+
err.message.must_match 'Invalid value "#<Class:'
84+
err.message.must_match '" for type "Date"'
85+
end
86+
end
87+
end
88+
end

0 commit comments

Comments
 (0)