Module: SlotMachine::Slots

Included in:
Slots, TimeSlots
Defined in:
lib/slot_machine/slots.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/slot_machine/slots.rb', line 4

def self.included(base)
  base.class_eval do
    def self.slot_class
      @slot_class ||= name.gsub(/s$/, "").split("::").inject(Object) do |mod, name|
        mod.const_get name
      end
    end
  end
end

Instance Method Details

#+(other) ⇒ Object



26
27
28
29
30
31
# File 'lib/slot_machine/slots.rb', line 26

def +(other)
  unless other.is_a?(Range) || other.is_a?(Array) || other.class == self.class.slot_class || other.class == self.class
    raise ArgumentError, "Either subtract a Range, an Array, #{self.class.slot_class.name} or #{self.class.name} instance (#{other.class.name} given)"
  end
  self.class.new add(other)
end

#-(other) ⇒ Object



33
34
35
36
37
38
# File 'lib/slot_machine/slots.rb', line 33

def -(other)
  unless other.is_a?(Range) || other.is_a?(Array) || other.class == self.class.slot_class || other.class == self.class
    raise ArgumentError, "Either subtract a Range, an Array, #{self.class.slot_class.name} or #{self.class.name} instance (#{other.class.name} given)"
  end
  self.class.new subtract(other)
end

#==(other) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/slot_machine/slots.rb', line 40

def ==(other)
  ((self.class == other.class) || (other_is_an_array = other.is_a?(Array))) && begin
    range_slots.size == (other_range_slots = other_is_an_array ? other : other.range_slots).size && begin
      range_slots.each_with_index do |range_slot, index|
        return false unless range_slot == other_range_slots[index]
      end
      true
    end
  end
end

#initialize(*ranges_or_range_slots) ⇒ Object



14
15
16
# File 'lib/slot_machine/slots.rb', line 14

def initialize(*ranges_or_range_slots)
  @range_slots = to_range_slots(add(ranges_or_range_slots.flatten))
end

#match(other, interval = nil) ⇒ Object



22
23
24
# File 'lib/slot_machine/slots.rb', line 22

def match(other, interval = nil)
  range_slots.collect{|range_slot| range_slot.match other, interval}.flatten.uniq
end

#range_slotsObject



18
19
20
# File 'lib/slot_machine/slots.rb', line 18

def range_slots
  @range_slots || []
end