Sha256: 1650654c9566c94b12a468f8c93755a80c75c027bb70bb8bfb1136751de03bc7

Contents?: true

Size: 1.36 KB

Versions: 3

Compression:

Stored size: 1.36 KB

Contents

module Boty
  # Public: contains de descriptive information that was associated with an
  # action via the `Bot#desc` method.
  #
  # Besides that also keep tracks of the regex for the associated Action, it is
  # used to describe the handler in case no command name and/or description was
  # passed via `Bot#desc`.
  #
  # The `ActionDescription` object will be stored inside the `Action#desc`. A
  # good usage example can be found in the `scripts/knows.rb` script.
  #
  # Examples:
  #
  #   # scripts/omg.rb
  #   desc "X omg!", "Make the bot scream X omgs."
  #   hear(/(\d+ )?omg!/i) do |how_many|
  #     how_many.times do say "LOL!" end
  #   end
  #
  #   # => generates an action with a description like this:
  #   ActionDescription.new "X omg!",
  #                         description: "Make the bot scream X omgs."
  #                         regex: /(\d+ )?omg!/i
  class ActionDescription
    attr_reader :command, :description
    attr_writer :regex

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

      @regex = regex
    end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
boty-1.0.1 lib/boty/action_description.rb
boty-1.0.0 lib/boty/action_description.rb
boty-0.2.0 lib/boty/action_description.rb