Sha256: d0e59942b89e9a6082e2104bd3477793d9c6a1a65e7d0d14d6ac5c730b3bd064

Contents?: true

Size: 1.64 KB

Versions: 6

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

require 'command_kit/env'

module CommandKit
  module Env
    #
    # Provides access to the `HOME` environment variable.
    #
    # ## Environment Variables
    #
    # * `HOME` - The absolute path to the user's home directory.
    #
    module Home
      include Env

      #
      # @api private
      #
      module ModuleMethods
        #
        # Extends {ClassMethods} or {ModuleMethods}, depending on whether
        # {Env::Home} is being included into a class or a module..
        #
        # @param [Class, Module] context
        #   The class or module which is including {Home}.
        #
        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
        #
        # The default home directory.
        #
        # @return [String]
        #
        # @api semipublic
        #
        def home_dir
          Gem.user_home
        end
      end

      # The home directory.
      #
      # @return [String]
      #
      # @api public
      attr_reader :home_dir

      #
      # Initializes {#home_dir} to either `env['HOME']` or
      # {ClassMethods#home_dir self.class.home_dir}.
      #
      # @param [Hash{Symbol => Object}] kwargs
      #   Additional keyword arguments.
      #
      # @api public
      #
      def initialize(**kwargs)
        super(**kwargs)

        @home_dir = env.fetch('HOME') { self.class.home_dir }
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
command_kit-0.4.0 lib/command_kit/env/home.rb
command_kit-0.3.0 lib/command_kit/env/home.rb
command_kit-0.2.2 lib/command_kit/env/home.rb
command_kit-0.2.1 lib/command_kit/env/home.rb
command_kit-0.2.0 lib/command_kit/env/home.rb
command_kit-0.1.0 lib/command_kit/env/home.rb