Skip to content

Commit b106c91

Browse files
nTraumodlp
authored andcommitted
Allow passed points to be any object that is enumerable
Previous to this change the `raw_points` passed to `Simplifier#process` had to be an array. This commit loosens the requirement by checking wether the object is enumerable / iterable, thus allowing non-arrays to be passed (e.g. ActiveRecord relations!).
1 parent 169ae50 commit b106c91

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

lib/simplify_rb.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
module SimplifyRb
77
class Simplifier
88
def process(raw_points, tolerance = 1, highest_quality = false)
9-
raise ArgumentError.new('Points must be an array') unless raw_points.is_a? Array
9+
raise ArgumentError.new('raw_points must be enumerable') unless raw_points.is_a? Enumerable
1010

1111
return raw_points if raw_points.length <= 1
1212

spec/simplify_rb_spec.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,19 @@
4949
end
5050
end
5151

52-
describe 'unexpected argument' do
53-
it 'raises an error if the points are not passsed as an array' do
54-
data = { x: 1, y: 2 }
55-
expect { subject.process(data) }.to raise_error(ArgumentError, 'Points must be an array')
52+
describe 'unexpected arguments' do
53+
context 'when raw_points is not enumerable' do
54+
it 'raises an ArgumentError' do
55+
data = Object.new
56+
expect { subject.process(data) }.to raise_error(ArgumentError, 'raw_points must be enumerable')
57+
end
58+
end
59+
60+
context "when raw_points is enumerable, but doesn't respond to x/y" do
61+
it 'raises an ArgumentError' do
62+
data = [{ foo: :bar }, { foo: :bar }]
63+
expect { subject.process(data) }.to raise_error(ArgumentError, 'Points must have :x and :y values')
64+
end
5665
end
5766
end
5867
end

0 commit comments

Comments
 (0)