Sha256: acf8419b2a50ffa974f0227b0b746918bd688c24c76ebaee6bee6e94a3634639

Contents?: true

Size: 1.3 KB

Versions: 3

Compression:

Stored size: 1.3 KB

Contents

module CommandKit
  #
  # Retrieves the current program name (`$PROGRAM_NAME`).
  #
  module ProgramName
    module ModuleMethods
      #
      # Extends {ClassMethods} or {ModuleMethods}, depending on whether
      # {ProgramName} is being included into a class or a module.
      #
      # @param [Class, Module] context
      #   The class or module which is including {ProgramName}.
      #
      def included(context)
        super(context)

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

    extend ModuleMethods

    #
    # Class-level methods.
    #
    module ClassMethods
      # List of `$PROGRAM_NAME`s that should be ignored.
      IGNORED_PROGRAM_NAMES = [
        '-e',   # ruby -e "..."
        'irb',  # running in irb
        'rspec' # running in rspec
      ]

      #
      # The current program name (`$PROGRAM_NAME`).
      #
      # @return [String, nil]
      #   The `$PROGRAM_NAME` or `nil` if the `$PROGRAM_NAME` is `-e`, `irb`,
      #   or `rspec`.
      #
      def program_name
        $PROGRAM_NAME unless IGNORED_PROGRAM_NAMES.include?($PROGRAM_NAME)
      end
    end

    #
    # @see ClassMethods#program_name
    #
    def program_name
      self.class.program_name
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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