Skip to content

Commit e274e12

Browse files
committed
1.) Always return an array for the color attribute.
2.) Allow searching by multiple colors by passing an array of color symbols to the color key in the hash passed to the method. (This actually breaks the symmetry between using with and chaining attribute methods)
1 parent 1fba0b0 commit e274e12

File tree

4 files changed

+29
-38
lines changed

4 files changed

+29
-38
lines changed

lib/pillboxr/attributes.rb

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def initialize(color_arg) # :nodoc:
5555
# puts "argument to method = #{color_arg}"
5656
@color = case color_arg
5757
when NilClass; raise ColorError
58+
when Array; color_arg.collect {|c| COLORS.fetch(c) }.join(';')
5859
when Symbol; COLORS.fetch(color_arg, color_arg)
5960
when String; COLORS.fetch(color_arg.to_sym, color_arg.to_sym)
6061
else raise "invalid arguments."
@@ -67,29 +68,14 @@ def to_param # :nodoc:
6768
end
6869
end
6970

70-
class Colors
71-
attr_accessor :colors
72-
73-
def initialize(color_arg)
74-
@colors = []
75-
color_arg.each do |c|
76-
@colors << Pillboxr::Attributes::Color.new(c)
77-
end
78-
return self
79-
end
80-
81-
def to_param
82-
@colors.collect(&:to_param).join
83-
end
84-
end
85-
8671
class Shape
8772
attr_accessor :shape
8873

8974
def initialize(shape_arg) # :nodoc:
9075
# puts "argument to method = #{shape_arg}"
9176
@shape = case shape_arg
9277
when NilClass; raise ShapeError
78+
when Array; shape_arg.collect {|s| SHAPES.fetch(s) }.join(';')
9379
when /^([Cc]{1}\d{5})+/; shape_arg # valid hex
9480
when Symbol; SHAPES.fetch(shape_arg, shape_arg)
9581
when String; SHAPES.fetch(shape_arg.to_sym, shape_arg.to_sym)
@@ -103,23 +89,6 @@ def to_param # :nodoc:
10389
end
10490
end
10591

106-
class Shapes
107-
attr_accessor :shapes
108-
109-
def initialize(shape_arg)
110-
@shapes = []
111-
112-
shape_arg.each do |s|
113-
@shapes << Pillboxr::Attributes::Shape.new(s)
114-
end
115-
return self
116-
end
117-
118-
def to_param
119-
@shapes.collect(&:to_param).join
120-
end
121-
end
122-
12392
class Productcode
12493
attr_accessor :product_code
12594

lib/pillboxr/pill.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ def initialize(params_hash)
1818
end
1919

2020
def color
21-
Pillboxr::Attributes::COLOR_CODES.fetch(@color.to_sym) { self.colors }
21+
self.colors
2222
end
2323

2424
def colors
25-
@color.split(';').map { |str| Pillboxr::Attributes::COLOR_CODES[str.to_sym] }
25+
@color.split(';').map { |str| Pillboxr::Attributes::COLOR_CODES.fetch(str.to_sym) }
2626
end
2727

2828
def shape
29-
Pillboxr::Attributes::SHAPE_CODES.fetch(@shape.to_sym) { self.shapes }
29+
self.shapes
3030
end
3131

3232
def shapes
33-
@shape.split(';').map { |str| Pillboxr::Attributes::SHAPE_CODES[str.to_sym] }
33+
@shape.split(';').map { |str| Pillboxr::Attributes::SHAPE_CODES.fetch(str.to_sym) }
3434
end
3535

3636
def score

test/pillboxr/pill_test.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,21 @@ def setup
77
@scored_pills = Pillboxr.score(3).get.pages.current.pills
88
end
99

10+
def test_color_returns_array
11+
@blue_pills.each do |pill|
12+
assert_instance_of(Array, pill.color)
13+
end
14+
end
15+
16+
def test_shape_returns_array
17+
@scored_pills.each do |pill|
18+
assert_instance_of(Array, pill.shape)
19+
end
20+
end
21+
1022
def test_accessor_methods
1123
@blue_pills.each do |pill|
12-
assert_includes(Array(pill.color), :blue)
24+
assert_includes(pill.color, :blue)
1325
assert_equal(true, pill.image?)
1426
end
1527

test/pillboxr/pillboxr_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ def test_all_valid_colors
5959
end
6060
end
6161

62+
def test_multiple_colors
63+
VCR.use_cassette(:multiple_colors) do
64+
r = Pillboxr.with({color: [:blue, :green]}).pages.current.pills
65+
r.each do |pill|
66+
assert_includes(pill.color, :blue)
67+
assert_includes(pill.color, :green)
68+
end
69+
end
70+
end
71+
6272
def test_all_valid_shapes
6373
VCR.use_cassette(:all_valid_shapes) do
6474
Pillboxr::Attributes::SHAPES.keys.each do |shape|

0 commit comments

Comments
 (0)