Class: TEF::ProgramSelection::SequenceCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/tef/ProgramSelection/SequenceCollection.rb

Overview

Sheet Sequence collection

This class is meant as a convenient container for Sequencing::Sheets. It automatically registers used IDs with the used Selector, and is also responsible for easily registering ProgramSheets.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(program_selector, sequence_runner) ⇒ SequenceCollection

Initialize a collection.

The passed TEF::ProgramSelection::Selector is used to register Sheet IDs, while the passed Sequencing::Player is used to start playing Sheets when using #play

Will set current_collection to self

Examples:

sheet_collection = SequenceCollection.new(programs, player);

ProgramSheet.new() do |s|
   s.add_key 'hello', ['portal', 'turret']

   # Write sheet contents here.
   # The ProgramSheet will self-register to the
   # created sheet collection.
end

sheet_collection.play(programs.fetch_string('hello'));


50
51
52
53
54
55
56
57
58
# File 'lib/tef/ProgramSelection/SequenceCollection.rb', line 50

def initialize(program_selector, sequence_runner)
	@program_selector = program_selector
	@sequence_runner = sequence_runner

	@known_programs = {}
	@sheet_opts = {}

	self.class.current_collection = self
end

Instance Attribute Details

#sheet_optsHash<ID, Hash> (readonly)

Returns Options to pass to specific sheets when instantiating them. Useful when re-using a Sequencing::Sheet for different programs.

Returns:

  • (Hash<ID, Hash>)

    Options to pass to specific sheets when instantiating them. Useful when re-using a Sequencing::Sheet for different programs.



18
19
20
# File 'lib/tef/ProgramSelection/SequenceCollection.rb', line 18

def sheet_opts
  @sheet_opts
end

Class Method Details

.current_collectionSequenceCollection

Returns Last Collection that was instantiated. Used by TEF::ProgramSelection to register itself.

Returns:



23
24
25
# File 'lib/tef/ProgramSelection/SequenceCollection.rb', line 23

def self.current_collection
	@current_collection
end

.current_collection=(n_collection) ⇒ Object



26
27
28
# File 'lib/tef/ProgramSelection/SequenceCollection.rb', line 26

def self.current_collection=(n_collection)
	@current_collection = n_collection
end

Instance Method Details

#[](key) ⇒ nil, ...

Returns Sheet matching the given ID, or nil.

Returns:



62
63
64
# File 'lib/tef/ProgramSelection/SequenceCollection.rb', line 62

def [](key)
	@known_programs[key]
end

#[]=(key, n_program) ⇒ Object

Register a given program.

Parameters:



73
74
75
76
# File 'lib/tef/ProgramSelection/SequenceCollection.rb', line 73

def []=(key, n_program)
	key = @program_selector.register_ID key
	@known_programs[key] = n_program
end

#play(key) ⇒ Object

Start playing the Sequencing::Sheet matching the given ID

This function will instantiate a new Sequencing::SheetSequence if the registered program was a ProgramSheet. It will pass the #sheet_opts matching its ID to the SheetSequence, and will additionally set the option’s :program_key value to the ID used here. This allows easy re-using of sheets by passing parameters via the sheet options.

The created sheet is then passed to Sequencing::Player#[]=, using either ProgramSheet#program_key or ‘default’ as key.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/tef/ProgramSelection/SequenceCollection.rb', line 90

def play(key)
	prog = @known_programs[key]
	return unless prog

	prog_key = prog.program_key if prog.is_a? ProgramSheet

	if prog.is_a? Sequencing::Sheet
		opts = @sheet_opts[key] || {}
		opts[:sheet] = prog
		opts[:program_key] = key

		prog = Sequencing::SheetSequence.new(Time.now(), 1, **opts)
	end
	prog_key ||= 'default'

	@sequence_runner[prog_key] = prog
end