class Nenv::Environment

Public Class Methods

new(namespace = nil) click to toggle source
# File lib/nenv/environment.rb, line 21
def initialize(namespace = nil)
  @namespace = (namespace ? namespace.upcase : nil)
end

Private Class Methods

_create_env_accessor(klass, meth, &block) click to toggle source
# File lib/nenv/environment.rb, line 44
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
_create_env_reader(klass, meth, &block) click to toggle source
# File lib/nenv/environment.rb, line 66
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
_create_env_writer(klass, meth, &block) click to toggle source
# File lib/nenv/environment.rb, line 56
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
_fail_if_accessor_exists(klass, meth) click to toggle source
# File lib/nenv/environment.rb, line 76
def _fail_if_accessor_exists(klass, meth)
  fail(AlreadyExistsError, meth) if klass.method_defined?(meth)
end
create_method(meth, &block) click to toggle source
# File lib/nenv/environment.rb, line 40
def create_method(meth, &block)
  _create_env_accessor(self, meth, &block)
end

Public Instance Methods

create_method(meth, &block) click to toggle source
# File lib/nenv/environment.rb, line 25
def create_method(meth, &block)
  self.class._create_env_accessor(singleton_class, meth, &block)
end

Private Instance Methods

_namespaced_sanitize(meth) click to toggle source
# File lib/nenv/environment.rb, line 35
def _namespaced_sanitize(meth)
  [@namespace, _sanitize(meth)].compact.join('_')
end
_sanitize(meth) click to toggle source
# File lib/nenv/environment.rb, line 31
def _sanitize(meth)
  meth.to_s[/^([^=?]*)[=?]?$/, 1].upcase
end