Sha256: fff55d1cacd0f4c39bade1b9d0156d91fd93d35c9bdad5909b0e07194fd5e304

Contents?: true

Size: 1.51 KB

Versions: 3

Compression:

Stored size: 1.51 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

      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]
        #
        def home_dir
          Gem.user_home
        end
      end

      # The home directory.
      #
      # @return [String]
      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.
      #
      def initialize(**kwargs)
        super(**kwargs)

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

Version data entries

3 entries across 3 versions & 1 rubygems

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