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: ===

Implements equality for the environment.

Compares by name, so an environment equals its name as a Symbol or a String, and equals another environment with the same name.

equal? is deliberately not aliased here: in Ruby it means object identity and overriding it broke that contract in both directions.



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

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

#development?Boolean

Returns true if the environment is development.

Returns:

  • (Boolean)


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

def development? = name == :development

#eql?(other) ⇒ Boolean

Value equality, kept consistent with #hash so an environment behaves as a Hash key.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


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

def eql?(other) = other.is_a?(Environment) && name == other.name

#hashInteger

Returns:

  • (Integer)


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

def hash = name.hash

#inspectString

Returns the name of the environment as a string.

Returns:

  • (String)


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

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.



87
88
89
90
91
92
93
94
# File 'lib/lennarb/environment.rb', line 87

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_sString

Returns the name of the environment as a string.

Returns:

  • (String)


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

def to_s = name.to_s

#to_symSymbol

Returns the name of the environment as a symbol.

Returns:

  • (Symbol)


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

def to_sym = name