Class: Rapid::Model::ComponentCreator

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Common::Types::Validators
Defined in:
lib/ury_rapid/model/component_creator.rb

Overview

A creator for stock model components

ComponentCreator contains several methods for building commonly used components for models. It does not register those components with a registrar; to do this, wrap the ComponentCreator in a ComponentCreatorWrapper that calls the registrar as a hook.

Instance Method Summary collapse

Methods included from Common::Types::Validators

invalid, validate_marker, validate_symbol, validate_volume

Constructor Details

#initializeComponentCreator

Initialises a ComponentCreator


19
20
# File 'lib/ury_rapid/model/component_creator.rb', line 19

def initialize
end

Instance Method Details

#constant(value, handler_target) ⇒ Constant

Creates an arbitrary constant object.

ComponentCreator contains methods for building various stock constants, which validate their values before instantiation. Use of those is recommended where available.

Examples:

Creating a constant with value 3 and handler target :target.

# If any response handlers are registered as operating on :target,
# this tree will have those handlers attached to it.
creator.constant(3, :target)

Parameters:

  • value (Object)

    The value of the constant.

  • handler_target (Symbol)

    The handler target of the constant.

Returns:

  • (Constant)

    A Constant component, holding the given value.


119
120
121
# File 'lib/ury_rapid/model/component_creator.rb', line 119

def constant(value, handler_target)
  Rapid::Model::Constant.new(handler_target, value)
end

#item(options) ⇒ Item

Creates a new playlist item

Parameters:

  • options (Hash)

    A hash containing the keys :type, :name, :origin, and :duration, which correspond to the type, name, origin, and duration of the item respectively.

Returns:

  • (Item)

    An Item model object holding the playlist item.


79
80
81
82
83
84
85
86
# File 'lib/ury_rapid/model/component_creator.rb', line 79

def item(options)
  Rapid::Model::Item.new(
    item_type(options),
    item_name(options),
    item_origin(options),
    item_duration(options)
  )
end

#list(handler_target) ⇒ ListModelObject

Creates an empty list component

A list is a model object that contains an ordered sequence of zero or more model objects. Each child of a list takes its current index (a natural number starting from zero) as its ID, and the IDs of list children change if objects are inserted or removed before them.

Examples:

Creating a list with handler target :target.

# If any response handlers are registered as operating on :target,
# this list will have those handlers attached to it.
creator.list(:target)

Parameters:

  • handler_target (Symbol)

    The tag used to identify this list to potential handlers.

Returns:


156
157
158
# File 'lib/ury_rapid/model/component_creator.rb', line 156

def list(handler_target)
  ListModelObject.new(handler_target)
end

#load_state(value) ⇒ Constant

Creates a component holding a load state

To change the value of the resulting object, replace it with a new load_state component.

Parameters:

  • value (Symbol)

    The value of the load state component.

Returns:

  • (Constant)

    A Constant model object holding a load state.


30
31
32
# File 'lib/ury_rapid/model/component_creator.rb', line 30

def load_state(value)
  validate_then_constant(:validate_load_state, value, :load_state)
end

#log(logger) ⇒ Log

Creates a new logger model interface

Examples:

Creating a log component for the logger 'logger'.

creator.log(logger)

Parameters:

  • logger (Object)

    An object that implements the standard library Logger's API.

Returns:

  • (Log)

    A Log object that serves as a model interface to the logger.


97
98
99
100
# File 'lib/ury_rapid/model/component_creator.rb', line 97

def log(logger)
  fail('Nil logger given.') if logger.nil?
  Rapid::Model::Log.new(logger)
end

#marker(type, value) ⇒ Constant

Creates a component holding a position marker

To change the value of the resulting object, replace it with a new marker component.

Parameters:

  • type (Symbol)

    The type symbol of the marker.

  • value (Numeric)

    The value of the marker component.

Returns:

  • (Constant)

    A Constant model object holding a volume.


67
68
69
# File 'lib/ury_rapid/model/component_creator.rb', line 67

def marker(type, value)
  validate_then_constant(:validate_marker, value, type)
end

#play_state(value) ⇒ Constant

Creates a component holding a play state

To change the value of the resulting object, replace it with a new play_state component.

Parameters:

  • value (Symbol)

    The value of the play state component.

Returns:

  • (Constant)

    A Constant model object holding a play state.


42
43
44
# File 'lib/ury_rapid/model/component_creator.rb', line 42

def play_state(value)
  validate_then_constant(:validate_play_state, value, :state)
end

#tree(handler_target) ⇒ HashModelObject

Creates an empty tree component

A tree is a model object that can contain zero or more other model objects, each identified by an arbitrary ID.

Examples:

Creating a tree with handler target :target.

# If any response handlers are registered as operating on :target,
# this tree will have those handlers attached to it.
creator.tree(:target)

Parameters:

  • handler_target (Symbol)

    The tag used to identify this tree to potential handlers.

Returns:


137
138
139
# File 'lib/ury_rapid/model/component_creator.rb', line 137

def tree(handler_target)
  HashModelObject.new(handler_target)
end

#volume(value) ⇒ Constant

Creates a component holding a volume

To change the value of the resulting object, replace it with a new volume component.

Parameters:

  • value (Numeric)

    The value of the volume component.

Returns:

  • (Constant)

    A Constant model object holding a volume.


54
55
56
# File 'lib/ury_rapid/model/component_creator.rb', line 54

def volume(value)
  validate_then_constant(:validate_volume, value, :volume)
end