Skip to content

Commit

Permalink
Add string to array of integers conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Jul 13, 2020
1 parent 955d471 commit c2dbc35
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
26 changes: 26 additions & 0 deletions lib/necromancer/converters/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,30 @@ def call(value, strict: config.strict)
end
end

class StringToIntegerArrayConverter < Converter
# @example
# converter.call("1,2,3") # => [1, 2, 3]
#
# @api public
def call(string, strict: config.strict)
array_converter = StringToArrayConverter.new(:string, :array)
array = array_converter.(string, strict: strict)
int_converter = ArrayToIntegerArrayConverter.new(:array, :integers)
int_converter.(array, strict: strict)
end
end

class ArrayToIntegerArrayConverter < Converter
# @example
# converter.call(["1", "2", "3"]) # => [1, 2, 3]
#
# @api public
def call(array, strict: config.strict)
int_converter = NumericConverters::StringToIntegerConverter.new(:string, :integer)
array.map { |val| int_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 @@ -121,6 +145,8 @@ def self.load(conversions)
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 ArrayToBooleanConverter.new(:array, :boolean)
conversions.register ObjectToArrayConverter.new(:object, :array)
Expand Down
2 changes: 2 additions & 0 deletions spec/unit/conversions/to_hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
'string->float',
'string->hash',
'string->integer',
'string->integers',
'string->ints',
'string->numeric',
'string->range',
'string->time',
Expand Down
7 changes: 6 additions & 1 deletion spec/unit/convert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
end

it "allows replacing #to with #>> call" do
expect(converter.convert("1,2,3") >> :array).to eq(%w[1 2 3])
expect(converter.convert("1,2,3") >> :integers).to eq([1,2,3])
end

it "allows to specify object as conversion target" do
Expand All @@ -37,6 +37,11 @@
expect(converter.convert("t,f,t").to(:bools)).to eq([true,false,true])
end

it "converts string to array of integers" do
expect(converter.convert("1,2,3").to(:integers)).to eq([1,2,3])
expect(converter.convert("1,2,3").to(:ints)).to eq([1,2,3])
end

it "converts array to numeric " do
expect(converter.convert(['1','2.3','3.0']).to(:numeric)).to eq([1,2.3,3.0])
end
Expand Down
33 changes: 33 additions & 0 deletions spec/unit/converters/array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,37 @@
"'unknown' could not be converted from `string` into `boolean`")
end
end

describe ":string -> :integers/:ints" do
subject(:converter) { described_class::StringToIntegerArrayConverter.new }

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

it "fails to convert in strict mode" do
expect {
converter.("1,unknown", strict: true)
}.to raise_error(Necromancer::ConversionTypeError,
"'unknown' could not be converted from `string` into `integer`")
end
end

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

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

0 comments on commit c2dbc35

Please sign in to comment.