Class: Spring

Inherits:
Object show all
Includes:
Observable
Defined in:
lib/spring.rb

Constant Summary collapse

GET =
:get
SIGN_UP =
:sign_up

Instance Method Summary collapse

Methods included from Observable

#add_observers, included, #list_observers, #notify_observer, #observer?

Constructor Details

#initialize(*items) ⇒ Spring

Returns a new instance of Spring.



20
21
22
23
24
# File 'lib/spring.rb', line 20

def initialize(*items)
  @q = *items
  @mutex = Mutex.new
  @collecter = []
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @mutex.synchronize { return @q.empty? }
end

#get(collecter_id) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/spring.rb', line 26

def get(collecter_id)
  @mutex.synchronize do
    if @collecter.include?(collecter_id)
      item = @q.shift
      changed(true)
      notify_observers(self.object_id, GET, collecter_id)
      return item
    end
  end
end

#num_collecterObject



57
58
59
# File 'lib/spring.rb', line 57

def num_collecter
  @mutex.synchronize { @collecter.length }
end

#sign_up(collecter_id) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/spring.rb', line 46

def (collecter_id)
  @mutex.synchronize do
    unless @collecter.include?(collecter_id)
      @collecter << collecter_id
      changed(true)
      notify_observers(self.object_id, SIGN_UP, collecter_id)
      return nil
    end
  end
end

#sizeObject Also known as: length



37
38
39
# File 'lib/spring.rb', line 37

def size
  @mutex.synchronize { return @q.size }
end