Sha256: 7069b8651d39c446641ee6e3f223cdba374a1c1c69d5eef924e246c413689fa3

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

class Sinclair
  module EnvSettable
    # @api private
    # @author darthjee
    #
    # Env setting methods builder
    #
    # This builder does the magic of adding methods
    # that will fetch variables from env or a default value
    class Builder < Sinclair
      # @param klass [Class] Class that will receive the methods
      # @param prefix [String] Env keys prefix
      # @param (see EnvSettable#with_settings)
      def initialize(klass, prefix, *settings_name, **defaults)
        super(klass, prefix: prefix)

        @settings = Hash[settings_name.map { |name| [name] }]

        @settings.merge!(defaults)

        add_all_methods
      end

      private

      attr_reader :settings
      # @method settings
      # @private
      # @api private
      #
      # Settings map with default values
      #
      # @return [Hash<Symbol,Object>]

      delegate :prefix, to: :options_object
      # @method prefix
      # @private
      # @api private
      #
      # Env keys prefix
      #
      # @return [String]

      # @private
      # @api private
      #
      # Process all settings adding the methods
      #
      # @return (see settings)
      def add_all_methods
        settings.each do |name, value|
          key = [prefix, name].compact.join('_').to_s.upcase

          add_class_method(name) do
            ENV[key] || value
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sinclair-1.6.0 lib/sinclair/env_settable/builder.rb
sinclair-1.5.2 lib/sinclair/env_settable/builder.rb
sinclair-1.5.1 lib/sinclair/env_settable/builder.rb