Skip to content

Commit

Permalink
Add list conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Mar 28, 2020
1 parent dce0b44 commit 76d7c53
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/tty/option/conversions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ def self.raise_invalid_argument(conv_name, val)
pairs
end
end

conversions.keys.each do |type|
next if type =~ /list|array/

[:"#{type}_list", :"#{type}_array", :"#{type}s"].each do |new_type|
convert new_type do |val|
conversions[:list].(val).map do |obj|
conversions[type].(obj)
end
end
end
end
end # Conversions
end # Option
end # TTY
15 changes: 15 additions & 0 deletions spec/unit/conversions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,21 @@
expect(described_class[:list].(input)).to eq(obj)
end
end

{
[:int_list, "1,2,3"] => [1, 2, 3],
[:ints, "1,2,3"] => [1, 2, 3],
[:float_list, "1,2,3"] => [1.0, 2.0, 3.0],
[:floats, "1,2,3"] => [1.0, 2.0, 3.0],
[:bool_list, "t,t,f"] => [true, true, false],
[:bools, "t,t,f"] => [true, true, false],
[:symbols, "a,b,c"] => [:a, :b, :c],
[:regexps, "a,b,c"] => [/a/, /b/, /c/]
}.each do |(type, input), obj|
it "converts #{input.inspect} to #{obj.inspect}" do
expect(described_class[type].(input)).to eq(obj)
end
end
end

context ":map" do
Expand Down

0 comments on commit 76d7c53

Please sign in to comment.