Sha256: baed7f73c4dfd63e3305bfe4d44253d08176b6146eac2e7403a18d9cde39fda4

Contents?: true

Size: 733 Bytes

Versions: 1

Compression:

Stored size: 733 Bytes

Contents

module Ellen
  class Action
    def self.prefix_pattern(robot_name)
      /\A@?#{Regexp.escape(robot_name)}:?\s*/
    end

    attr_reader :options, :pattern

    def initialize(pattern, options = {})
      @pattern = pattern
      @options = options
    end

    def call(handler, message)
      handler.send(name, message) if message.match pattern_with(handler.robot.name)
    end

    def all?
      !!options[:all]
    end

    def description
      options[:description] || "(no description)"
    end

    def name
      options[:name]
    end

    private

    def pattern_with(robot_name)
      if all?
        /\A#{pattern}/
      else
        /#{self.class.prefix_pattern(robot_name)}#{pattern}/
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ellen-0.2.4 lib/ellen/action.rb