lib/percentable/percent.rb in percentable-1.0.2 vs lib/percentable/percent.rb in percentable-1.1.2
- old
+ new
@@ -35,15 +35,17 @@
def <=> other
to_f <=> other.to_f
end
def * other
- case other
- when Percent
+ if other.is_a? Percent
self.class.new(to_f * other.value)
+ elsif other.respond_to? :coerce
+ a, b = other.coerce(self)
+ a * b
else
- self.class.new(value * other.to_f)
+ raise TypeError, "#{other.class} can't be coerced into Percent"
end
end
[:+, :-, :/].each do |operator|
define_method operator do |other|
@@ -68,15 +70,10 @@
fail TypeError, 'must inherit from Numeric'
end
end
def coerce other
- case other
- when Numeric
- [CoercedPercent.new(self), other]
- else
- fail TypeError, "#{self.class} can't be coerced into #{other.class}"
- end
+ [CoercedPercent.new(self), other]
end
end
end
Percent = Percentable::Percent