Class: SpringSet

Inherits:
Object show all
Defined in:
lib/spring_set.rb

Instance Method Summary collapse

Constructor Details

#initialize(*springs) ⇒ SpringSet

Returns a new instance of SpringSet.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/spring_set.rb', line 13

def initialize(*springs)
  @springs = {}
  springs.flatten.each do |s|
    @springs[s.object_id] = {
      :spring => s,
      :num_collecter => s.num_collecter,
      :size => s.size
    }
    s.add_observer(self)
  end
end

Instance Method Details

#eachObject



66
67
68
# File 'lib/spring_set.rb', line 66

def each
  @springs.values.each { |v| yield(v[:spring]) }
end

#get_most_busyObject



38
39
40
# File 'lib/spring_set.rb', line 38

def get_most_busy
  calc_stat(:num_collecter) { |a, b| a > b }
end

#get_most_emptyObject



46
47
48
# File 'lib/spring_set.rb', line 46

def get_most_empty
  calc_stat(:size) { |a, b| a < b }
end

#get_most_freeObject



34
35
36
# File 'lib/spring_set.rb', line 34

def get_most_free
  calc_stat(:num_collecter) { |a, b| a < b }
end

#get_most_fullObject



42
43
44
# File 'lib/spring_set.rb', line 42

def get_most_full
  calc_stat(:size) { |a, b| a > b }
end

#num_collecter(spring_id) ⇒ Object



56
57
58
# File 'lib/spring_set.rb', line 56

def num_collecter(spring_id)
  @springs[spring_id][:num_collecter]
end

#sizeObject Also known as: length



50
51
52
# File 'lib/spring_set.rb', line 50

def size
  @springs.size
end

#spring_size(spring_id) ⇒ Object Also known as: spring_length



60
61
62
# File 'lib/spring_set.rb', line 60

def spring_size(spring_id)
  @springs[spring_id][:size]
end

#update(spring_id, msg, collecter_id) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/spring_set.rb', line 25

def update(spring_id, msg, collecter_id)
  case msg
  when Spring::SIGN_UP
    @springs[spring_id][:num_collecter] += 1
  when Spring::GET
    @springs[spring_id][:size] -= 1 if @springs[spring_id][:size] > 0
  end
end