Sha256: 27e0c5703f8d75be1881ff12ae76d01004be535907ae2aeedee2e14e620d0421

Contents?: true

Size: 1.88 KB

Versions: 23

Compression:

Stored size: 1.88 KB

Contents

require 'nenv/environment/dumper'
require 'nenv/environment/loader'

module Nenv
  class Environment
    class Error < ArgumentError
    end

    class MethodError < Error
      def initialize(meth)
        @meth = meth
      end
    end

    class AlreadyExistsError < MethodError
      def message
        format('Method %s already exists', @meth.inspect)
      end
    end

    def initialize(namespace = nil)
      @namespace = (namespace ? namespace.upcase : nil)
    end

    def create_method(meth, &block)
      self.class._create_env_accessor(singleton_class, meth, &block)
    end

    private

    def _sanitize(meth)
      meth.to_s[/^([^=?]*)[=?]?$/, 1].upcase
    end

    def _namespaced_sanitize(meth)
      [@namespace, _sanitize(meth)].compact.join('_')
    end

    class << self
      def create_method(meth, &block)
        _create_env_accessor(self, meth, &block)
      end

      def _create_env_accessor(klass, meth, &block)
        _fail_if_accessor_exists(klass, meth)

        if meth.to_s.end_with? '='
          _create_env_writer(klass, meth, &block)
        else
          _create_env_reader(klass, meth, &block)
        end
      end

      private

      def _create_env_writer(klass, meth, &block)
        env_name = nil
        dumper = nil
        klass.send(:define_method, meth) do |raw_value|
          env_name ||= _namespaced_sanitize(meth)
          dumper ||= Dumper.setup(&block)
          ENV[env_name] = dumper.(raw_value)
        end
      end

      def _create_env_reader(klass, meth, &block)
        env_name = nil
        loader = nil
        klass.send(:define_method, meth) do
          env_name ||= _namespaced_sanitize(meth)
          loader ||= Loader.setup(meth, &block)
          loader.(ENV[env_name])
        end
      end

      def _fail_if_accessor_exists(klass, meth)
        fail(AlreadyExistsError, meth) if klass.method_defined?(meth)
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 8 rubygems

Version Path
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/nenv-0.3.0/lib/nenv/environment.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/nenv-0.3.0/lib/nenv/environment.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/nenv-0.3.0/lib/nenv/environment.rb
qiita_org-0.1.35 gems/ruby/2.7.0/gems/nenv-0.3.0/lib/nenv/environment.rb
qiita_org-0.1.34 gems/ruby/2.7.0/gems/nenv-0.3.0/lib/nenv/environment.rb
qiita_org-0.1.33 gems/ruby/2.7.0/gems/nenv-0.3.0/lib/nenv/environment.rb
qiita_org-0.1.32 gems/ruby/2.7.0/gems/nenv-0.3.0/lib/nenv/environment.rb
qiita_org-0.1.31 gems/ruby/2.7.0/gems/nenv-0.3.0/lib/nenv/environment.rb
qiita_org-0.1.30 gems/ruby/2.7.0/gems/nenv-0.3.0/lib/nenv/environment.rb
qiita_org-0.1.29 gems/ruby/2.7.0/gems/nenv-0.3.0/lib/nenv/environment.rb
qiita_org-0.1.28 gems/ruby/2.7.0/gems/nenv-0.3.0/lib/nenv/environment.rb
qiita_org-0.1.27 gems/ruby/2.7.0/gems/nenv-0.3.0/lib/nenv/environment.rb
qiita_org-0.1.26 gems/ruby/2.7.0/gems/nenv-0.3.0/lib/nenv/environment.rb
qiita_org-0.1.25 gems/ruby/2.7.0/gems/nenv-0.3.0/lib/nenv/environment.rb
honeybadger-4.5.3 vendor/bundle/ruby/2.6.0/gems/nenv-0.3.0/lib/nenv/environment.rb
alimentos-alu0100945645-0.1.0 vendor/bundle/ruby/2.3.0/gems/nenv-0.3.0/lib/nenv/environment.rb
alimentos-alu0100945645-1.0.0 vendor/bundle/ruby/2.3.0/gems/nenv-0.3.0/lib/nenv/environment.rb
comiditaULL-0.1.1 vendor/bundle/ruby/2.3.0/gems/nenv-0.3.0/lib/nenv/environment.rb
comidita_ull-0.1.1 vendor/bundle/ruby/2.3.0/gems/nenv-0.3.0/lib/nenv/environment.rb
comidita_ull-0.1.0 vendor/bundle/ruby/2.3.0/gems/nenv-0.3.0/lib/nenv/environment.rb