Sha256: 1fc1961fa55754f0f5147a24fd6496ec4f4c2e490cc7b6fb95146cc63927ef81

Contents?: true

Size: 1.28 KB

Versions: 4

Compression:

Stored size: 1.28 KB

Contents

module Boty
  class Action
    include Boty::Logger
    attr_reader :regex, :desc, :action

    class Description
      attr_reader :command, :description
      attr_writer :regex

      def initialize(command, description: nil, regex: nil)
        if description.nil?
          @description = command
        else
          @command, @description = command, description
        end

        @regex = regex
      end

      def command
        return @command if @command

        match = /\?(i?)-(mx|mix):(.*)\)/.match @regex.to_s
        "/#{match[3]}/#{match[1]}"
      end
    end

    def initialize(regex, desc, &action)
      desc = Hash(desc)
      description = Description.new(desc[:command],
                                    description: desc[:description],
                                    regex: regex)
      @regex, @desc, @action = regex, description, action
    end

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

    def execute(bot, message)
      dsl = ScriptDSL.new(bot)
      if match = message.match!(regex)
        matches = Array(match)
        matches.shift
        dsl.instance_exec(*matches, &action)
      end
    rescue => e
      logger.error e.message
      raise e
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
boty-0.0.17.1 lib/boty/action.rb
boty-0.0.17 lib/boty/action.rb
boty-0.0.16 lib/boty/action.rb
boty-0.0.15 lib/boty/action.rb