Skip to content

Commit

Permalink
Raise error on unsupported options
Browse files Browse the repository at this point in the history
Theis will be usefull to eliminate all
the possible confusion going on what options
are and are not supported by chronic.

One such example is:
mojombo/chronic#182 (comment)

It is a good idea to tell people know about it in advance
if they provide invalid or unsupported options.
  • Loading branch information
BFF76o authored and dnagir committed Jul 2, 2014
1 parent a5d9d43 commit 123443c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/chronic/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Parser
# two digit year is `now + x years` it's assumed to be the
# future, `now - x years` is assumed to be the past.
def initialize(options = {})
validate_options!(options)
@options = DEFAULT_OPTIONS.merge(options)
@now = options.delete(:now) || Chronic.time_class.now
end
Expand Down Expand Up @@ -154,6 +155,14 @@ def definitions(options = {})

private


def validate_options!(options)
given = options.keys.map(&:to_s).sort
allowed = DEFAULT_OPTIONS.keys.map(&:to_s).sort
non_permitted = given - allowed
raise ArgumentError, "Unsupported option(s): #{non_permitted.join(', ')}" if non_permitted.any?
end

def tokenize(text, options)
text = pre_normalize(text)
tokens = text.split(' ').map { |word| Token.new(word) }
Expand Down
19 changes: 19 additions & 0 deletions test/test_chronic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,25 @@ def test_datetime
end
end

def test_valid_options
options = {
:context => :future,
:now => nil,
:hours24 => nil,
:week_start => :sunday,
:guess => true,
:ambiguous_time_range => 6,
:endian_precedence => [:middle, :little],
:ambiguous_year_future_bias => 50
}
refute_nil Chronic.parse('now', options)
end

def test_invalid_options
assert_raises(ArgumentError) { Chronic.parse('now', foo: 'boo') }
assert_raises(ArgumentError) { Chronic.parse('now', time_class: Time) }
end

def test_activesupport
=begin
# ActiveSupport needs MiniTest '~> 4.2' which conflicts with '~> 5.0'
Expand Down

0 comments on commit 123443c

Please sign in to comment.