Sha256: 510e340581a482235861d1de7677f92e80d3174027e003e7c883d57e6f501e90

Contents?: true

Size: 1.98 KB

Versions: 3

Compression:

Stored size: 1.98 KB

Contents

require 'command_kit/inflector'

module CommandKit
  #
  # Defines or derives a command class'es command-name.
  #
  # ## Examples
  #
  # ### Implicit
  #
  #     class MyCmd
  #
  #       include CommandKit::CommandName
  #
  #     end
  #
  #     MyCmd.command_name
  #     # => "my_cmd"
  #
  # ### Explicit
  #
  #     class MyCmd
  #
  #       include CommandKit::CommandName
  #
  #       commnad_name 'foo-cmd'
  #
  #     end
  #
  #     MyCmd.command_name
  #     # => "foo-cmd"
  #
  module CommandName
    module ModuleMethods
      #
      # Extends {ClassMethods} or {ModuleMethods}, depending on whether
      # {CommandName} is being included into a class or a module.
      #
      # @param [Class, Module] context
      #   The class or module which is including {CommandName}.
      #
      def included(context)
        super(context)

        if context.class == Module
          context.extend ModuleMethods
        else
          context.extend ClassMethods
        end
      end
    end

    extend ModuleMethods

    #
    # Defines class-level methods.
    #
    module ClassMethods
      #
      # Derives the command name from the class name.
      #
      # @param [String, nil] new_command_name
      #   If given a command name argument, it will override the derived
      #   command name.
      #
      # @return [String]
      #
      def command_name(new_command_name=nil)
        if new_command_name
          @command_name = new_command_name.to_s
        else
          @command_name || Inflector.underscore(Inflector.demodularize(name))
        end
      end
    end

    # The commands name.
    #
    # @return [String]
    attr_reader :command_name

    #
    # Initializes command_name.
    #
    # @param [String] command_name
    #   Overrides the command name. Defaults to
    #   {ClassMethods#command_name self.class.command_name}.
    #
    def initialize(command_name: self.class.command_name, **kwargs)
      @command_name = command_name

      super(**kwargs)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
command_kit-0.1.0.rc1 lib/command_kit/command_name.rb
command_kit-0.1.0.pre2 lib/command_kit/command_name.rb
command_kit-0.1.0.pre1 lib/command_kit/command_name.rb