Class: Hanami::Env Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/env.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Encapsulate access to ENV

Since:

  • 0.9.0

Instance Method Summary collapse

Constructor Details

#initialize(env: ENV) ⇒ Hanami::Env

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new instance

Parameters:

  • env (#[], #[]=) (defaults to: ENV)

    a Hash like object. It defaults to ENV

Since:

  • 0.9.0



20
21
22
# File 'lib/hanami/env.rb', line 20

def initialize(env: ENV)
  @env = env
end

Instance Method Details

#[](key) ⇒ String, NilClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return a value, if found

Parameters:

  • key (String)

    the key

Returns:

  • (String, NilClass)

    the value, if found

Since:

  • 0.9.0



32
33
34
# File 'lib/hanami/env.rb', line 32

def [](key)
  @env[key]
end

#[]=(key, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets a value

Parameters:

  • key (String)

    the key

  • value (String)

    the value

Since:

  • 0.9.0



43
44
45
# File 'lib/hanami/env.rb', line 43

def []=(key, value)
  @env[key] = value
end

#load!(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Loads a dotenv file and updates self

Parameters:

  • path (String, Pathname)

    the path to the dotenv file

Returns:

  • void

Since:

  • 0.9.0



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/hanami/env.rb', line 55

def load!(path)
  return unless defined?(Dotenv::Parser)

  contents = ::File.open(path, "rb:bom|utf-8", &:read)
  parsed   = Dotenv::Parser.call(contents)

  parsed.each do |k, v|
    next if @env.has_key?(k)

    @env[k] = v
  end
  nil
end