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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Instructions::Composite

#all_off, #play

Constructor Details

#initialize(ins, outs, &block) ⇒ Context

Returns a new instance of Context.



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

def initialize(ins, outs, &block)
  
  @state = State.new(ins, outs)
  
  @instructions = {
    :process => Instructions::Process.new(@state),
    :input => Instructions::Input.new(@state),      
    :message => Instructions::Message.new(@state),
    :output => Instructions::Output.new(@state),
    :sticky => Instructions::Sticky.new(@state),
    :sysex => Instructions::SysEx.new(@state)
  }
   
  edit(&block) unless block.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a, &b) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/micromidi/context.rb', line 40

def method_missing(m, *a, &b)
  delegated = false
  outp = nil
  options = a.last.kind_of?(Hash) ? a.last : {}
  do_output = options[:output] || true
  [@instructions[:sysex], @instructions[:message], @instructions[:process]].each do |dsl|
    if dsl.respond_to?(m)
      msg = dsl.send(m, *a, &b)
      outp = @state.auto_output && do_output ? @instructions[:output].output(msg) : msg
      delegated = true
    end
  end
  unless delegated
    [@instructions[:input], @instructions[:output], @instructions[:sticky]].each do |dsl| 
      if dsl.respond_to?(m)
        outp = dsl.send(m, *a, &b)
        delegated = true
      end
    end
  end
  @state.record(m, a, b, outp)
  delegated ? outp : super
end

Instance Attribute Details

#stateObject (readonly)

Returns the value of attribute state.



11
12
13
# File 'lib/micromidi/context.rb', line 11

def state
  @state
end

Instance Method Details

#edit(&block) ⇒ Object

open a block for editing/live coding in this Context



32
33
34
# File 'lib/micromidi/context.rb', line 32

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

#repeatObject Also known as: r



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

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