Class: TinyConveyor::Belt

Inherits:
Object
  • Object
show all
Includes:
Concurrent::Async
Defined in:
lib/tiny_conveyor/belt.rb

Overview

Thread manager, allow creation and execution of parcels

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBelt

Returns newly created belt.



11
12
13
14
15
16
# File 'lib/tiny_conveyor/belt.rb', line 11

def initialize
  super()
  @parcels = []
  @threads = []
  @state = :waiting
end

Instance Attribute Details

#parcelsObject (readonly)

Returns the value of attribute parcels.



8
9
10
# File 'lib/tiny_conveyor/belt.rb', line 8

def parcels
  @parcels
end

Instance Method Details

#add_parcel(name, description, task) ⇒ Array<Parcel>

Returns pending parcels.

Parameters:

  • name (string)

    name of the task

  • description (string)

    small description of the task

  • action (Proc)

    action to run in separate thread

Returns:

  • (Array<Parcel>)

    pending parcels



22
23
24
25
# File 'lib/tiny_conveyor/belt.rb', line 22

def add_parcel(name, description, task)
  parcel = Parcel.new(name, description, task)
  @parcels << parcel
end

#remove_parcel(parcel) ⇒ Object



49
50
51
52
# File 'lib/tiny_conveyor/belt.rb', line 49

def remove_parcel(parcel)
  new_parcels_array = @parcels.reject { |p| p == parcel }
  @parcels = new_parcels_array
end

#remove_parcel_by_uuid(uuid) ⇒ Array<Parcel>

Returns pending parcels.

Parameters:

  • uuid (string)

    uuid of the parcel to remove

Returns:

  • (Array<Parcel>)

    pending parcels



29
30
31
32
33
34
# File 'lib/tiny_conveyor/belt.rb', line 29

def remove_parcel_by_uuid(uuid)
  parcel = @parcels.find { |p| p.uuid == uuid }
  return @parcels if parcel.nil?

  remove_parcel(parcel)
end

#run_belt_entirelyObject



40
41
42
# File 'lib/tiny_conveyor/belt.rb', line 40

def run_belt_entirely
  execute until @parcels.empty?
end

#running?boolean

Returns true if the belt is running, false otherwhise.

Returns:

  • (boolean)

    true if the belt is running, false otherwhise



45
46
47
# File 'lib/tiny_conveyor/belt.rb', line 45

def running?
  @state == :running
end

#start_beltObject



36
37
38
# File 'lib/tiny_conveyor/belt.rb', line 36

def start_belt
  self.async.run_belt_entirely
end