Sha256: 8f318f24d663bcb0e6f014eb2deb743ee7bdef72e6cc68c757bb3024e47d25fc

Contents?: true

Size: 957 Bytes

Versions: 6

Compression:

Stored size: 957 Bytes

Contents

module Attentive
  class Match
    attr_reader :listener, :phrase, :message, :match_start, :match_end

    def initialize(phrase, attributes={})
      @phrase = phrase.to_s
      @match_data = attributes.fetch(:match_data, {})
      @match_start = attributes.fetch(:match_start)
      @match_end = attributes.fetch(:match_end)
      @message = attributes.fetch(:message)
      @listener = attributes[:listener]
    end

    def matched?(variable_name)
      !@match_data[variable_name.to_s].nil?
    end

    def [](variable_name)
      @match_data.fetch variable_name.to_s
    rescue KeyError
      raise KeyError, "#{$!.message} in #{inspect}"
    end

    def to_s
      @phrase
    end

    def to_h
      @match_data
    end

    def replace_with(tokens)
      message[match_start...match_end] = tokens
      match_start + tokens.length
    end

    def inspect
      "#<#{self.class.name} #{@match_data.inspect} #{phrase.inspect}>"
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
attentive-0.3.9 lib/attentive/match.rb
attentive-0.3.8 lib/attentive/match.rb
attentive-0.3.7 lib/attentive/match.rb
attentive-0.3.6 lib/attentive/match.rb
attentive-0.3.5 lib/attentive/match.rb
attentive-0.3.4 lib/attentive/match.rb