Sha256: 6ff44bafa20772a075e276c638e1cf3f4806d665ac2f3390e5af346a2fa4f3d9

Contents?: true

Size: 936 Bytes

Versions: 2

Compression:

Stored size: 936 Bytes

Contents

module Boty
  # Public: Wrap the idea of something that should happen when some regex is
  # matched.
  #
  #
  class Action
    include Boty::Logger
    attr_reader :regex, :desc, :action

    def initialize(bot, regex, description, &action)
      if description
        description.regex = regex
      else
        description = ActionDescription.new(nil, regex: regex)
      end

      @dsl    = DSL.new bot
      @regex  = regex
      @desc   = description
      @action = action
    end

    def this?(regex, block)
      same_regex = regex == self.regex
      block ? same_regex && block == action : same_regex
    end

    def execute(message)
      action_call message.match!(regex)
    rescue => e
      logger.error e.message
      raise e
    end

    private

    def action_call(match)
      return unless match
      matches = Array(match)
      matches.shift
      @dsl.instance_exec(*matches, &action)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
boty-0.1.2 lib/boty/action.rb
boty-0.1.1 lib/boty/action.rb