class Nenv::Environment

Public Class Methods

create_method(meth, &block) click to toggle source
# File lib/nenv/environment.rb, line 25
def self.create_method(meth, &block)
  _create_env_method(self, meth, &block)
end
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_method(instance, meth, &block) click to toggle source
# File lib/nenv/environment.rb, line 39
def self._create_env_method(instance, meth, &block)
  _fail_if_exists(instance, meth)

  instance.send(:define_method, meth) do |*args|
    env_name = [@namespace, _sanitize(meth)].compact.join('_')

    if args.size == 1
      raw_value = args.first
      ENV[env_name] = Dumper.new.dump(raw_value, &block)
    else
      Loader.new(meth).load(ENV[env_name], &block)
    end
  end
end
_fail_if_exists(instance, meth) click to toggle source
# File lib/nenv/environment.rb, line 54
def self._fail_if_exists(instance, meth)
  fail(AlreadyExistsError, meth) if instance.instance_methods.include?(meth)
end

Public Instance Methods

create_method(meth, &block) click to toggle source
# File lib/nenv/environment.rb, line 29
def create_method(meth, &block)
  self.class._create_env_method(class << self; self; end, meth, &block)
end

Private Instance Methods

_sanitize(meth) click to toggle source
# File lib/nenv/environment.rb, line 35
def _sanitize(meth)
  meth.to_s[/^([^=?]*)[=?]?$/, 1].upcase
end