Class: MicroMIDI::Context

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Instructions::Composite
Defined in:
lib/micromidi/context.rb,
lib/micromidi/instructions/shorthand.rb

Overview

The DSL context

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Instructions::Composite

#all_off, #play

Constructor Details

#initialize(inputs, outputs, &block) ⇒ Context

Returns a new instance of Context.

Parameters:

  • inputs (Array<UniMIDI::Input>, UniMIDI::Input)
  • outputs (Array<UniMIDI::Output, IO>, IO, UniMIDI::Output)
  • block (Proc)

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/micromidi/context.rb', line 16

def initialize(inputs, outputs, &block)

  @state = State.new(inputs, outputs)

  @instructions = {
    :process => Instructions::Process.new(@state),
    :message => Instructions::Message.new(@state),
    :output => Instructions::Output.new(@state),
    :sticky => Instructions::Sticky.new(@state),
    :sysex => Instructions::SysEx.new(@state)
  }
  @instructions[:input] = Instructions::Input.new(@state) { |message| @instructions[:output].output(message) }

  edit(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Delegates a command to one of the instruction classes

Parameters:

  • method (Symbol)
  • args (*Object)
  • block (Proc)

Returns:

  • (Object)

52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/micromidi/context.rb', line 52

def method_missing(method, *args, &block)
  results = delegate(method, args, &block)
  if results.empty?
    super
  else
    messages = results.map do |result|
      @state.record(method, args, block, result[:message])
      @instructions[:output].output(result[:message]) if output?(result[:instruction_type])
      result[:message]
    end
    messages.compact.first
  end
end

Instance Attribute Details

#stateObject (readonly)

Returns the value of attribute state


9
10
11
# File 'lib/micromidi/context.rb', line 9

def state
  @state
end

Instance Method Details

#edit(&block) ⇒ Object

Eval a block for editing/live coding in this context

Parameters:

  • block (Proc)

Returns:

  • (Object)

35
36
37
# File 'lib/micromidi/context.rb', line 35

def edit(&block)
  instance_eval(&block)
end

#repeatObject Also known as: r

Repeat the last instruction in the history

Returns:

  • (Object)

41
42
43
44
45
# File 'lib/micromidi/context.rb', line 41

def repeat
  unless @state.last_command.nil?
    send(@state.last_command[:method], *@state.last_command[:args])
  end
end