Module: SlotMachine::Slot

Included in:
Slot, TimeSlot
Defined in:
lib/slot_machine/slot.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/slot_machine/slot.rb', line 4

def self.included(base)
  base.class_eval do
    attr_reader :start, :end, :length

    def self.interval(value)
      @interval = value
    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

Instance Method Details

#+(other) ⇒ Object



64
65
66
# File 'lib/slot_machine/slot.rb', line 64

def +(other)
  self.class.slots_class.new(self) + other
end

#-(other) ⇒ Object



68
69
70
# File 'lib/slot_machine/slot.rb', line 68

def -(other)
  self.class.slots_class.new(self) - other
end

#==(other) ⇒ Object



72
73
74
# File 'lib/slot_machine/slot.rb', line 72

def ==(other)
  self.class == other.class && self.start == other.start && self.end == other.end && self.length == other.length
end

#end=(value) ⇒ Object



47
48
49
# File 'lib/slot_machine/slot.rb', line 47

def end=(value)
  @end = end!(value) if range?
end

#initialize(range_or_length) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/slot_machine/slot.rb', line 24

def initialize(range_or_length)
  if range_or_length.is_a? Range
    @type = :range
    self.start = range_or_length.first
    self.end   = range_or_length.last
  else
    @type = :length
    self.length = range_or_length
  end
end

#inspectObject



76
77
78
79
80
81
82
83
84
85
# File 'lib/slot_machine/slot.rb', line 76

def inspect
  inspect_variables = begin
    if range?
      "@start=#{@start} @end=#{@end}"
    else
      "@length=#{@length}"
    end
  end
  "#<#{self.class.name} #{inspect_variables}>"
end

#length=(value) ⇒ Object



51
52
53
# File 'lib/slot_machine/slot.rb', line 51

def length=(value)
  @length = length!(value) if length?
end

#length?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/slot_machine/slot.rb', line 39

def length?
  @type == :length
end

#match(other, interval = nil) ⇒ Object

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
62
# File 'lib/slot_machine/slot.rb', line 55

def match(other, interval = nil)
  interval ||= self.class.default_interval
  raise ArgumentError, "Interval has to be greater than 0 (#{interval} given)" unless interval > 0
  unless self.class == other.class
    other = self.class.new other
  end
  match_compared to_compared, other.to_compared, interval
end

#range?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/slot_machine/slot.rb', line 35

def range?
  @type == :range
end

#start=(value) ⇒ Object



43
44
45
# File 'lib/slot_machine/slot.rb', line 43

def start=(value)
  @start = start!(value) if range?
end