Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
decanter (4.0.0)
decanter (4.1.0)
actionpack (>= 4.2.10)
activesupport
rails-html-sanitizer (>= 1.0.4)
Expand Down
1 change: 1 addition & 0 deletions lib/decanter/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ class Error < StandardError; end
class UnhandledKeysError < Error; end
class MissingRequiredInputValue < Error; end
class ParseError < Error; end
class ValueFormatError < Error; end
end
15 changes: 10 additions & 5 deletions lib/decanter/parser/date_parser.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
module Decanter
module Parser
class DateParser < ValueParser

allow Date

parser do |val, options|
raise Decanter::ParseError.new 'Expects a single value' if val.is_a? Array
next if (val.nil? || val === '')
raise Decanter::ParseError, 'Expects a single value' if val.is_a? Array
next if val.nil? || val === ''

parse_format = options.fetch(:parse_format, '%m/%d/%Y')
::Date.strptime(val, parse_format)
begin
::Date.strptime(val, parse_format)
rescue ArgumentError => e
Comment thread
oroth8 marked this conversation as resolved.
Outdated
raise Decanter::ValueFormatError, 'invalid Date value for format' if e.message == 'invalid date'

raise ArgumentError, e.message
Comment thread
oroth8 marked this conversation as resolved.
Outdated
end
end
end
end
end

14 changes: 10 additions & 4 deletions lib/decanter/parser/datetime_parser.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
module Decanter
module Parser
class DateTimeParser < ValueParser

allow DateTime

parser do |val, options|
raise Decanter::ParseError.new 'Expects a single value' if val.is_a? Array
next if (val.nil? || val === '')
raise Decanter::ParseError, 'Expects a single value' if val.is_a? Array
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line can be removed since this parser inherits from ValueParser, which raises an error if the value is an array

next if val.nil? || val === ''

parse_format = options.fetch(:parse_format, '%m/%d/%Y %I:%M:%S %p')
::DateTime.strptime(val, parse_format)
begin
::DateTime.strptime(val, parse_format)
rescue ArgumentError => e
raise Decanter::ValueFormatError, 'invalid DateTime value for format' if e.message == 'invalid date'

raise ArgumentError, e.message
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/decanter/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Decanter
VERSION = '4.0.0'.freeze
VERSION = '4.1.0'.freeze
end
9 changes: 8 additions & 1 deletion spec/decanter/parser/date_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@
context 'with an invalid date string' do
it 'raises an Argument Error' do
expect { parser.parse(name, '2-21-1990') }
.to raise_error(ArgumentError, 'invalid date')
.to raise_error(Decanter::ValueFormatError, 'invalid Date value for format')
end
end

context 'with a invalid date string and custom format' do
it 'raises an Argument Error' do
expect { parser.parse(name, '2-21-1990', parse_format: '%d-%m-%Y') }
.to raise_error(Decanter::ValueFormatError, 'invalid Date value for format')
end
end

Expand Down
9 changes: 8 additions & 1 deletion spec/decanter/parser/datetime_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@
context 'with an invalid date string' do
it 'raises an Argument Error' do
expect { parser.parse(name, '2-21-1990') }
.to raise_error(ArgumentError, 'invalid date')
.to raise_error(Decanter::ValueFormatError, 'invalid DateTime value for format')
end
end

context 'with a invalid date string and custom format' do
it 'raises an Argument Error' do
expect { parser.parse(name, '2-21-1990', parse_format: '%d-%m-%Y') }
.to raise_error(Decanter::ValueFormatError, 'invalid DateTime value for format')
end
end

Expand Down