Skip to content

Commit

Permalink
Change strict option to keyword argument
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Jun 29, 2020
1 parent 7e5bae2 commit ca43576
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 34 deletions.
2 changes: 1 addition & 1 deletion lib/necromancer/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def raise_conversion_type(value)

attr_accessor :convert

protected
# protected

attr_reader :config
end # Converter
Expand Down
17 changes: 7 additions & 10 deletions lib/necromancer/converters/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ class StringToArrayConverter < Converter
# converter.call("1 - 2 - 3") # => [1, 2, 3]
#
# @api public
def call(value, options = {})
strict = options.fetch(:strict, config.strict)
def call(value, strict: config.strict)
case value.to_s
when /^\s*?((\d+)(\s*(,|-)\s*)?)+\s*?$/
value.to_s.split($4).map(&:to_i)
Expand All @@ -42,10 +41,10 @@ class ArrayToNumericConverter < Converter
# the value to convert
#
# @api public
def call(value, options = {})
def call(value, strict: config.strict)
numeric_converter = NumericConverters::StringToNumericConverter.new(:string, :numeric)
value.reduce([]) do |acc, el|
acc << numeric_converter.call(el, **options)
acc << numeric_converter.call(el, strict: strict)
end
end
end
Expand All @@ -59,10 +58,10 @@ class ArrayToBooleanConverter < Converter
# the array value to boolean
#
# @api public
def call(value, options = {})
def call(value, strict: config.strict)
boolean_converter = BooleanConverters::StringToBooleanConverter.new(:string, :boolean)
value.reduce([]) do |acc, el|
acc << boolean_converter.call(el, options)
acc << boolean_converter.call(el, strict: strict)
end
end
end
Expand All @@ -75,8 +74,7 @@ class ObjectToArrayConverter < Converter
# converter.call({x: 1}) # => [[:x, 1]]
#
# @api public
def call(value, options = {})
strict = options.fetch(:strict, config.strict)
def call(value, strict: config.strict)
begin
Array(value)
rescue
Expand All @@ -96,8 +94,7 @@ class ArrayToSetConverter < Converter
# the array to convert
#
# @api public
def call(value, options = {})
strict = options.fetch(:strict, config.strict)
def call(value, strict: config.strict)
begin
value.to_set
rescue
Expand Down
9 changes: 3 additions & 6 deletions lib/necromancer/converters/boolean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ class StringToBooleanConverter < Converter
# 0, f, F, FALSE, false, False, n, N, NO, no, No, off, OFF
#
# @api public
def call(value, options = {})
strict = options.fetch(:strict, config.strict)
def call(value, strict: config.strict)
case value.to_s
when TRUE_MATCHER then true
when FALSE_MATCHER then false
Expand All @@ -50,8 +49,7 @@ class IntegerToBooleanConverter < Converter
# converter.call(0) # => false
#
# @api public
def call(value, options = {})
strict = options.fetch(:strict, config.strict)
def call(value, strict: config.strict)
begin
!value.zero?
rescue
Expand All @@ -71,8 +69,7 @@ class BooleanToIntegerConverter < Converter
# converter.call(false) # => 0
#
# @api public
def call(value, options = {})
strict = options.fetch(:strict, config.strict)
def call(value, strict: config.strict)
if %w[TrueClass FalseClass].include?(value.class.name)
value ? 1 : 0
else
Expand Down
9 changes: 3 additions & 6 deletions lib/necromancer/converters/date_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class StringToDateConverter < Converter
# converter.call("12/11/2015") # => "2015-11-12"
#
# @api public
def call(value, options = {})
strict = options.fetch(:strict, config.strict)
def call(value, strict: config.strict)
Date.parse(value)
rescue
strict ? raise_conversion_type(value) : value
Expand All @@ -37,8 +36,7 @@ class StringToDateTimeConverter < Converter
# converer.call("1-1-2015 15:12:44") # => "2015-01-01T15:12:44+00:00"
#
# @api public
def call(value, options = {})
strict = options.fetch(:strict, config.strict)
def call(value, strict: config.strict)
DateTime.parse(value)
rescue
strict ? raise_conversion_type(value) : value
Expand All @@ -57,8 +55,7 @@ class StringToTimeConverter < Converter
# converter.call("12:35") # => 2015-01-04 12:35:00 +0100
#
# @api public
def call(value, options = {})
strict = options.fetch(:strict, config.strict)
def call(value, strict: config.strict)
Time.parse(value)
rescue
strict ? raise_conversion_type(value) : value
Expand Down
13 changes: 5 additions & 8 deletions lib/necromancer/converters/numeric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ class StringToIntegerConverter < Converter
# converter.call("1abc") # => 1
#
# @api public
def call(value, **options)
strict = options.fetch(:strict, config.strict)
def call(value, strict: config.strict)
Integer(value)
rescue
strict ? raise_conversion_type(value) : value.to_i
Expand Down Expand Up @@ -47,8 +46,7 @@ class StringToFloatConverter < Converter
# converter.call("1.2") # => 1.2
#
# @api public
def call(value, **options)
strict = options.fetch(:strict, config.strict)
def call(value, strict: config.strict)
Float(value)
rescue
strict ? raise_conversion_type(value) : value.to_f
Expand All @@ -66,13 +64,12 @@ class StringToNumericConverter < Converter
# converter.call("1") # => 1
#
# @api public
def call(value, **options)
strict = options.fetch(:strict, config.strict)
def call(value, strict: config.strict)
case value
when INTEGER_MATCHER
StringToIntegerConverter.new(:string, :integer).call(value, **options)
StringToIntegerConverter.new(:string, :integer).call(value, strict: strict)
when FLOAT_MATCHER
StringToFloatConverter.new(:string, :float).call(value, **options)
StringToFloatConverter.new(:string, :float).call(value, strict: strict)
else
strict ? raise_conversion_type(value) : value
end
Expand Down
3 changes: 1 addition & 2 deletions lib/necromancer/converters/range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ class StringToRangeConverter < Converter
# converter.call("0-9") # => (0..9)
#
# @api public
def call(value, options = {})
strict = options.fetch(:strict, config.strict)
def call(value, strict: config.strict)
if match = value.match(SINGLE_DIGIT_MATCHER)
digit = cast_to_num(match[:digit])
::Range.new(digit, digit)
Expand Down
2 changes: 1 addition & 1 deletion lib/necromancer/null_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
module Necromancer
# A pass through converter
class NullConverter < Converter
def call(value, **options)
def call(value, strict: config.strict)
value
end
end # NullConverter
Expand Down

0 comments on commit ca43576

Please sign in to comment.