Class: MusaLCEServer::Control

Inherits:
Object
  • Object
show all
Defined in:
lib/surface.rb

Overview

Abstract base for all controls on a Surface.

Subclasses declare a Control.type_name matching the inventory string received from the surface, expose typed state accessors, and implement #emit_all_state to push their current state outbound.

Setting a property emits exactly one OSC +/musalce/surface/state/+ message; the setter is therefore the canonical mutation point. Direct manipulation of instance variables bypasses emission.

Direct Known Subclasses

Encoder, Toggle, Trigger

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event:, surface:) ⇒ Control

Returns a new instance of Control.



173
174
175
176
177
# File 'lib/surface.rb', line 173

def initialize(event:, surface:)
  @event = event
  @surface = surface
  @message = nil
end

Instance Attribute Details

#eventSymbol (readonly)

Returns the event name this control is bound to.

Returns:

  • (Symbol)

    the event name this control is bound to



168
169
170
# File 'lib/surface.rb', line 168

def event
  @event
end

#messageString?

Returns the displayable message, +nil+ if unset.

Returns:

  • (String, nil)

    the displayable message, +nil+ if unset



171
172
173
# File 'lib/surface.rb', line 171

def message
  @message
end

Class Method Details

.create(type, **kwargs) ⇒ Control

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Instantiates the right subclass for the given type.

Parameters:

  • type (Symbol)

Returns:

Raises:

  • (ArgumentError)

    if the type is unknown



239
240
241
242
243
244
245
246
# File 'lib/surface.rb', line 239

def self.create(type, **kwargs)
  case type.to_sym
  when :toggle  then Toggle.new(**kwargs)
  when :trigger then Trigger.new(**kwargs)
  when :encoder then Encoder.new(**kwargs)
  else raise ArgumentError, "Unknown control type: #{type.inspect}"
  end
end

.type_nameSymbol

Returns the inventory type identifier.

Returns:

  • (Symbol)

    the inventory type identifier

Raises:

  • (NotImplementedError)


230
231
232
# File 'lib/surface.rb', line 230

def self.type_name
  raise NotImplementedError, "#{self} must implement .type_name"
end

Instance Method Details

#emit_all_statevoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Re-emits every state property of this control. Called by the surface during +state_request+ or after inventory dumps.



225
226
227
# File 'lib/surface.rb', line 225

def emit_all_state
  emit(:message, @message.to_s) unless @message.nil?
end

#set(**attrs) ⇒ self

Sets multiple attributes in a single call. Each key must name a writable attribute of the receiver's type; unknown keys raise +ArgumentError+ so a typo can't silently no-op.

Order of assignment follows the kwargs hash insertion order (Ruby >= 1.9 guarantees insertion-ordered iteration). Each assignment goes through the regular setter, so each property emits its own +/musalce/surface/state/+ message. This is intentional: the wire protocol is per-property, and the plugin merges deltas into the rendered state, so two adjacent +/state/+ messages render exactly the same as a single batched one would.

Examples:

Toggle

surface[:launch_chorus].set(enabled: true, message: "Chorus on")

Encoder

surface[:cutoff].set(range: 0..127, value: 64, message: "Cutoff")

Parameters:

  • attrs (Hash{Symbol => Object})

Returns:

  • (self)

    for chaining

Raises:

  • (ArgumentError)

    if a key doesn't correspond to a writer



209
210
211
212
213
214
215
216
217
218
219
# File 'lib/surface.rb', line 209

def set(**attrs)
  attrs.each do |key, value|
    writer = :"#{key}="
    unless respond_to?(writer)
      raise ArgumentError,
            "#{self.class.type_name} control has no '#{key}' attribute"
    end
    public_send(writer, value)
  end
  self
end