class Cannonbol::OnSuccess

Public Class Methods

new(pattern, opts, &block) click to toggle source
# File lib/cannonbol/cannonbol.rb, line 205
def initialize(pattern, opts, &block)
  if opts.class == Hash
    if opts.first
      @capture_name = opts.first.first
      @initial_capture_value = opts.first.last
    end
  else
    @capture_name = opts
  end
  @pattern = pattern
  @block = block
end

Public Instance Methods

__match?(needle, thread_state = nil, starting_cursor = nil, s=[]) click to toggle source
# File lib/cannonbol/cannonbol.rb, line 218
def __match?(needle, thread_state = nil, starting_cursor = nil, s=[])
  needle.pull(thread_state)
  starting_cursor ||= needle.cursor
  if s = @pattern._match?(needle, *s)
    ending_cursor = needle.cursor-1
    push = needle.push(0) do
      match_string = MatchString.new(needle.string, starting_cursor, ending_cursor, needle.captures)
      capture_value = @capture_name && (needle.captures.has_key?(@capture_name) ? needle.captures[@capture_name] : @initial_capture_value)
      if @block
        match_string = @block.call(match_string, ending_cursor+1, capture_value)
      elsif capture_value.class == Array
        match_string = capture_value + [match_string]
      end
      needle.capture(@capture_name, match_string)
    end
    [ push, starting_cursor, s ]
  end
end