lib/slot_machine/slot.rb in slot_machine-0.1.0 vs lib/slot_machine/slot.rb in slot_machine-0.1.1

- old
+ new

@@ -10,10 +10,16 @@ end def self.default_interval @interval || 10 end + + def self.slots_class + @slots_class ||= "#{name}s".split("::").inject(Object) do |mod, name| + mod.const_get name + end + end end end def initialize(range_or_length) if range_or_length.is_a? Range @@ -53,10 +59,18 @@ other = self.class.new other end match_compared to_compared, other.to_compared, interval end + def +(other) + self.class.slots_class.new(self) + other + end + + def -(other) + self.class.slots_class.new(self) - other + end + def ==(other) self.class == other.class && self.start == other.start && self.end == other.end && self.length == other.length end def inspect @@ -70,10 +84,14 @@ "#<#{self.class.name} #{inspect_variables}>" end protected + def typecast(value) + value.to_s + end + def valid?(value) true end def to_compared @@ -123,10 +141,10 @@ def abs!(value) typecast! value, :>=, 0 end def typecast!(value, operator, compared) - Integer(value.to_s).tap do |value| + Integer(typecast(value)).tap do |value| raise ArgumentError, "Passed value should be #{operator} #{compared} (#{value} given)" if compared && !value.send(operator, compared) end end def valid!(value) \ No newline at end of file