Skip to content

Commit

Permalink
Add array to array of float conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Jul 13, 2020
1 parent 6cd30e2 commit 47bea47
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/necromancer/converters/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ def call(array, strict: config.strict)
end
end

class ArrayToFloatArrayConverter < Converter
# @example
# converter.call(["1", "2", "3"]) # => [1.0, 2.0, 3.0]
#
# @api public
def call(array, strict: config.strict)
float_converter = NumericConverters::StringToFloatConverter.new(:string, :float)
array.map { |val| float_converter.(val, strict: strict) }
end
end

# An object that converts an array to an array with numeric values
class ArrayToNumericConverter < Converter
# Convert an array to an array of numeric values
Expand Down Expand Up @@ -142,14 +153,20 @@ def call(value, strict: config.strict)

def self.load(conversions)
conversions.register NullConverter.new(:array, :array)

conversions.register StringToArrayConverter.new(:string, :array)
conversions.register StringToBoolArrayConverter.new(:string, :bools)
conversions.register StringToBoolArrayConverter.new(:string, :booleans)
conversions.register StringToIntegerArrayConverter.new(:string, :integers)
conversions.register StringToIntegerArrayConverter.new(:string, :ints)

conversions.register ArrayToNumericConverter.new(:array, :numeric)
conversions.register ArrayToIntegerArrayConverter.new(:array, :integers)
conversions.register ArrayToIntegerArrayConverter.new(:array, :ints)
conversions.register ArrayToFloatArrayConverter.new(:array, :floats)
conversions.register ArrayToBooleanConverter.new(:array, :booleans)
conversions.register ArrayToBooleanConverter.new(:array, :bools)

conversions.register ObjectToArrayConverter.new(:object, :array)
conversions.register ObjectToArrayConverter.new(:hash, :array)
end
Expand Down
3 changes: 3 additions & 0 deletions spec/unit/conversions/to_hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"array->array",
"array->booleans",
"array->bools",
"array->floats",
"array->integers",
"array->ints",
"array->numeric",
"boolean->boolean",
"boolean->integer",
Expand Down
13 changes: 13 additions & 0 deletions spec/unit/converters/array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@
end
end

describe ":array -> :floats" do
subject(:converter) { described_class::ArrayToFloatArrayConverter.new }

{
%w[1 2 3] => [1.0, 2.0, 3.0],
%w[1.2 2.3 3.4] => [1.2, 2.3, 3.4]
}.each do |input, obj|
it "converts #{input.inspect} to #{obj.inspect}" do
expect(converter.(input)).to eq(obj)
end
end
end

describe ":array -> :numeric" do
subject(:converter) { described_class::ArrayToNumericConverter.new(:array, :numeric) }

Expand Down

0 comments on commit 47bea47

Please sign in to comment.