Sha256: 7503385ab44b3cb68c125d9997648c2f14382b2033d461827cf3ca5de1ab39df

Contents?: true

Size: 1.07 KB

Versions: 14

Compression:

Stored size: 1.07 KB

Contents

module Ruboty
  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, options = {})
      if !!options[:missing] == missing? && message.match(pattern_with(handler.robot.name))
        !!handler.send(name, message)
      else
        false
      end
    end

    def all?
      !!options[:all]
    end

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

    def hidden?
      !!options[:hidden]
    end

    # @note :missing action is called only when any handler doesn't match given message.
    def missing?
      !!options[:missing]
    end

    def name
      options[:name]
    end

    def <=>(action)
      pattern.to_s <=> action.pattern.to_s
    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

14 entries across 14 versions & 1 rubygems

Version Path
ruboty-1.3.0 lib/ruboty/action.rb
ruboty-1.2.4 lib/ruboty/action.rb
ruboty-1.2.3 lib/ruboty/action.rb
ruboty-1.2.2 lib/ruboty/action.rb
ruboty-1.2.1 lib/ruboty/action.rb
ruboty-1.2.0 lib/ruboty/action.rb
ruboty-1.1.9 lib/ruboty/action.rb
ruboty-1.1.8 lib/ruboty/action.rb
ruboty-1.1.7 lib/ruboty/action.rb
ruboty-1.1.6 lib/ruboty/action.rb
ruboty-1.1.5 lib/ruboty/action.rb
ruboty-1.1.4 lib/ruboty/action.rb
ruboty-1.1.3 lib/ruboty/action.rb
ruboty-1.1.2 lib/ruboty/action.rb