class TEF::ProgramSelection::SequenceCollection

Sheet Sequence collection

This class is meant as a convenient container for {Sequencing::Sheet}s. It automatically registers used {ID}s with the used {Selector}, and is also responsible for easily registering {ProgramSheet}s.

Attributes

sheet_opts[R]

@return [Hash<ID, Hash>] Options to pass to

specific sheets when instantiating them.
Useful when re-using a {Sequencing::Sheet} for
different programs.

Public Class Methods

current_collection() click to toggle source

@return [SequenceCollection] Last Collection that

was instantiated. Used by {ProgramSelection} to
register itself.
# File lib/tef/ProgramSelection/SequenceCollection.rb, line 23
def self.current_collection
        @current_collection
end
current_collection=(n_collection) click to toggle source
# File lib/tef/ProgramSelection/SequenceCollection.rb, line 26
def self.current_collection=(n_collection)
        @current_collection = n_collection
end
new(program_selector, sequence_runner) click to toggle source

Initialize a collection.

The passed {Selector} is used to register Sheet {ID}s, while the passed {Sequencing::Player} is used to start playing Sheets when using {#play}

Will set {SequenceCollection#current_collection} to self

@example

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'));
# 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

Public Instance Methods

[](key) click to toggle source

@return [nil, Sequencing::Sheet, ProgramSheet] Sheet matching

the given {ID}, or nil.
# File lib/tef/ProgramSelection/SequenceCollection.rb, line 62
def [](key)
        @known_programs[key]
end
[]=(key, n_program) click to toggle source

Register a given program.

@param [ID] key {ID} to register the program under.

Will be registered with the {Selector} passed to the
constructor.

@param [Sequencing::Sheet, ProgramSheet] n_program New program

to register.
# 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) click to toggle source

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.

# 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