Class: PM::Trigger

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

Overview

A Trigger executes code when it sees a particular array of bytes. Instruments have zero or more triggers.

Since we want to save them to files, we store the text representation as well.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes, code_chunk) ⇒ Trigger

Returns a new instance of Trigger.



12
13
14
# File 'lib/patchmaster/trigger.rb', line 12

def initialize(bytes, code_chunk)
  @bytes, @code_chunk = bytes, code_chunk
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



16
17
18
# File 'lib/patchmaster/trigger.rb', line 16

def method_missing(sym, *args)
  PM::PatchMaster.instance.send(sym, *args)
end

Instance Attribute Details

#bytesObject

Returns the value of attribute bytes.



10
11
12
# File 'lib/patchmaster/trigger.rb', line 10

def bytes
  @bytes
end

#code_chunkObject

Returns the value of attribute code_chunk.



10
11
12
# File 'lib/patchmaster/trigger.rb', line 10

def code_chunk
  @code_chunk
end

Instance Method Details

#signal(bytes) ⇒ Object

If bytes matches our @bytes array then run @code_chunk.



21
22
23
24
25
26
27
# File 'lib/patchmaster/trigger.rb', line 21

def signal(bytes)
  if bytes == @bytes
    pm = PM::PatchMaster.instance
    @code_chunk.run(pm)
    pm.gui.refresh if pm.gui
  end
end

#to_sObject



29
30
31
# File 'lib/patchmaster/trigger.rb', line 29

def to_s
  "#{@bytes.inspect} => #{(@code_chunk.text || '# no block text found').gsub(/\n\s*/, '; ')}"
end