Class: TinyConveyor::Belt
- Inherits:
-
Object
- Object
- TinyConveyor::Belt
- Includes:
- Concurrent::Async
- Defined in:
- lib/tiny_conveyor/belt.rb
Overview
Thread manager, allow creation and execution of parcels
Instance Attribute Summary collapse
-
#parcels ⇒ Object
readonly
Returns the value of attribute parcels.
Instance Method Summary collapse
-
#add_parcel(name, description, task) ⇒ Array<Parcel>
Pending parcels.
-
#initialize ⇒ Belt
constructor
Newly created belt.
- #remove_parcel(parcel) ⇒ Object
-
#remove_parcel_by_uuid(uuid) ⇒ Array<Parcel>
Pending parcels.
- #run_belt_entirely ⇒ Object
-
#running? ⇒ boolean
True if the belt is running, false otherwhise.
- #start_belt ⇒ Object
Constructor Details
#initialize ⇒ Belt
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
#parcels ⇒ Object (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.
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.
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_entirely ⇒ Object
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.
45 46 47 |
# File 'lib/tiny_conveyor/belt.rb', line 45 def running? @state == :running end |
#start_belt ⇒ Object
36 37 38 |
# File 'lib/tiny_conveyor/belt.rb', line 36 def start_belt self.async.run_belt_entirely end |