Class: Lennarb::Environment
- Inherits:
-
Object
- Object
- Lennarb::Environment
- Defined in:
- lib/lennarb/environment.rb
Overview
Manage the environment of the application.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the name of the environment.
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #===)
Implements equality for the environment.
-
#development? ⇒ Boolean
Returns true if the environment is development.
-
#eql?(other) ⇒ Boolean
Value equality, kept consistent with #hash so an environment behaves as a Hash key.
- #hash ⇒ Integer
-
#initialize(name) ⇒ Environment
constructor
Initialize the environment.
-
#inspect ⇒ String
Returns the name of the environment as a string.
-
#local? ⇒ Boolean
Returns true if the environment is local (either
testordevelopment). -
#on(*envs) ⇒ Object
Yields a block if the environment is the same as the given environment.
-
#production? ⇒ Boolean
Returns true if the environment is production.
-
#test? ⇒ Boolean
Returns true if the environment is test.
-
#to_s ⇒ String
Returns the name of the environment as a string.
-
#to_sym ⇒ Symbol
Returns the name of the environment as a symbol.
Constructor Details
#initialize(name) ⇒ Environment
Initialize the environment. @param name [String, Symbol] The name of the environment.
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
#name ⇒ Object (readonly)
Returns the name of the environment.
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.
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.
58 |
# File 'lib/lennarb/environment.rb', line 58 def eql?(other) = other.is_a?(Environment) && name == other.name |
#hash ⇒ Integer
62 |
# File 'lib/lennarb/environment.rb', line 62 def hash = name.hash |
#inspect ⇒ String
Returns the name of the environment as a 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).
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
:anyor:all. - To match local environments use
:local.
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.
35 |
# File 'lib/lennarb/environment.rb', line 35 def production? = name == :production |
#test? ⇒ Boolean
Returns true if the environment is test.
31 |
# File 'lib/lennarb/environment.rb', line 31 def test? = name == :test |
#to_s ⇒ String
Returns the name of the environment as a string.
72 |
# File 'lib/lennarb/environment.rb', line 72 def to_s = name.to_s |
#to_sym ⇒ Symbol
Returns the name of the environment as a symbol.
67 |
# File 'lib/lennarb/environment.rb', line 67 def to_sym = name |