Sha256: b83282e52511607e7b775289423706b03837f6a783363bb9c00235d246bfea23

Contents?: true

Size: 744 Bytes

Versions: 3

Compression:

Stored size: 744 Bytes

Contents

module Cognition
  class Matcher
    attr_accessor :trigger, :action, :response, :help

    def initialize(trigger, help = 'Undocumented', &action)
      raise ArgumentError, 'matcher must have a trigger' unless trigger
      raise ArgumentError, 'matcher must have a action' unless action
      @trigger = trigger
      @help = help
      @action = action
    end

    def attempt(msg)
      return false unless matches?(msg)

      run(msg)
    end

    def run(msg)
      @response = action.call(msg)
    end

    def matches?(msg)
      if trigger.is_a? String
        return true if trigger == msg.command
      elsif trigger.is_a? Regexp
        return true if (@match = trigger.match msg.command)
      end
      false
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cognition-1.0.0 lib/cognition/matcher.rb
cognition-0.1.0 lib/cognition/matcher.rb
cognition-0.0.1 lib/cognition/matcher.rb