Class: Environment

Inherits:
Object show all
Defined in:
lib/madvertise/ext/environment.rb

Overview

A simple convenience class to support multiple environments in which a program can run (e.g. development, production, etc).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = nil) ⇒ Environment

Create a new Environment instance with the corresponding key in the ENV hash.

Parameters:

  • key (String) (defaults to: nil)

    The key in ENV to contain the current program environment.



14
15
16
# File 'lib/madvertise/ext/environment.rb', line 14

def initialize(key=nil)
  @key = key
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



6
7
8
# File 'lib/madvertise/ext/environment.rb', line 6

def key
  @key
end

Instance Method Details

#dev?Boolean

Return true if the current environment is development.

Returns:

  • (Boolean)


40
41
42
# File 'lib/madvertise/ext/environment.rb', line 40

def dev?
  to_sym == :development
end

#modeString

Retreive the current environment mode.

Returns:

  • (String)

    The current environment mode.



21
22
23
24
25
# File 'lib/madvertise/ext/environment.rb', line 21

def mode
  ENV[@key] || 'development'
rescue
  'development'
end

#prod?Boolean

Return true if the current environment is production.

Returns:

  • (Boolean)


35
36
37
# File 'lib/madvertise/ext/environment.rb', line 35

def prod?
  to_sym == :production
end

#set(value) ⇒ Object

Set the environment mode.

Parameters:

  • The (String)

    new environment mode.



52
53
54
# File 'lib/madvertise/ext/environment.rb', line 52

def set(value)
  ENV[@key] = value.to_s
end

#test?Boolean

Return true if the current environment is test.

Returns:

  • (Boolean)


45
46
47
# File 'lib/madvertise/ext/environment.rb', line 45

def test?
  to_sym == :test
end

#to_symSymbol

Retrieve the current environment mode and convert it to a symbol.

Returns:

  • (Symbol)

    The current environment mode.



30
31
32
# File 'lib/madvertise/ext/environment.rb', line 30

def to_sym
  mode.to_sym
end