Class: Amberletters::OutputTrigger

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

Instance Attribute Summary

Attributes inherited from Trigger

#exclusive, #interruption, #logger, #options, #time_to_live

Instance Method Summary collapse

Constructor Details

#initialize(pattern = //, options = {}, &block) ⇒ OutputTrigger

Returns a new instance of OutputTrigger.



81
82
83
84
85
# File 'lib/amberletters.rb', line 81

def initialize(pattern=//, options={}, &block)
  super(options, &block)
  options[:operation] ||= :all
  @pattern = pattern
end

Instance Method Details

#call(process) ⇒ Object



91
92
93
94
95
96
# File 'lib/amberletters.rb', line 91

def call(process)
  case @pattern
  when Array then match_multiple(process)
  else match_one(process)
  end
end

#match_multiple(process) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/amberletters.rb', line 110

def match_multiple(process)
  op = options[:operation]
  raise "Invalid operation #{op.inspect}" unless [:any, :all].include?(op)
  scanner = process.output_buffer
  @logger.debug "matching #{op} of multiple patterns against #{scanner.rest.inspect}"
  starting_pos = scanner.pos
  ending_pos   = starting_pos
  result = @pattern.send("#{op}?") {|pattern|
    scanner.pos = starting_pos
    if (char_count = scanner.skip_until(pattern))
      ending_pos = [ending_pos, starting_pos + char_count].max
    end
  }
  if result
    scanner.pos = ending_pos
    true
  else
    scanner.pos = starting_pos
    false
  end
end

#match_one(process) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/amberletters.rb', line 98

def match_one(process)
  scanner = process.output_buffer
  @logger.debug "matching #{@pattern.inspect} against #{scanner.rest.inspect}"
  if scanner.scan_until(@pattern)
    @logger.debug "matched #{@pattern.inspect}"
    @block.call(self, process, scanner)
    true
  else
    false
  end
end

#to_sObject



87
88
89
# File 'lib/amberletters.rb', line 87

def to_s
  "output matching #{@pattern.inspect}"
end