Class: Lennarb::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/lennarb/environment.rb

Overview

Manage the environment of the application.

Examples:

app.env.development? # => true
app.env.test? # => false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Environment

Initialize the environment.

@param name [String, Symbol] The name of the environment.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
# File 'lib/lennarb/environment.rb', line 17

def initialize(name)
  @name = name.to_sym

  return if NAMES.include?(@name)

  raise ArgumentError, "Invalid environment: #{@name.inspect}"
end

Instance Attribute Details

#nameObject (readonly)

Returns the name of the environment.

Parameters:

  • name (Symbol)


12
13
14
# File 'lib/lennarb/environment.rb', line 12

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?, equal?, ===

Implements equality for the environment.



43
# File 'lib/lennarb/environment.rb', line 43

def ==(other) = name == other || name.to_s == other

#development?Boolean

Returns true if the environment is development.

Returns:

  • (Boolean)


27
# File 'lib/lennarb/environment.rb', line 27

def development? = name == :development

#inspectObject

Returns the name of the environment as a string.



60
# File 'lib/lennarb/environment.rb', line 60

def inspect = to_s.inspect

#local?Boolean

Returns true if the environment is local (either ‘test` or `development`).

Returns:

  • (Boolean)


39
# File 'lib/lennarb/environment.rb', line 39

def local? = test? || development?

#on(*envs) ⇒ Object

Yields a block if the environment is the same as the given environment.

  • To match all environments use ‘:any` or `:all`.

  • To match local environments use ‘:local`.

Examples:

app.env.on(:development) do
  # Code to run in development
end

Parameters:

  • envs (Array<Symbol>)

    The environment(s) to check.



71
72
73
74
75
76
77
78
# File 'lib/lennarb/environment.rb', line 71

def on(*envs)
  matched = envs.include?(:any) ||
    envs.include?(:all) ||
    envs.include?(name) ||
    (envs.include?(:local) && local?)

  yield if matched
end

#production?Boolean

Returns true if the environment is production.

Returns:

  • (Boolean)


35
# File 'lib/lennarb/environment.rb', line 35

def production? = name == :production

#test?Boolean

Returns true if the environment is test.

Returns:

  • (Boolean)


31
# File 'lib/lennarb/environment.rb', line 31

def test? = name == :test

#to_sObject

Returns the name of the environment as a string.



56
# File 'lib/lennarb/environment.rb', line 56

def to_s = name.to_s

#to_symObject

Returns the name of the environment as a symbol.



51
# File 'lib/lennarb/environment.rb', line 51

def to_sym = name