Class: EnvStyle::Environment

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

Overview

Resolves the current environment name and the tint color it maps to.

Resolution order mirrors env.style:

config.environment -> ENV["ENV_STYLE_ENV"] -> Rails.env

Production is never tinted, and a disabled configuration or an environment with no color leaves the favicon untouched.

Constant Summary collapse

PRODUCTION =
"production"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: EnvStyle.config, rails_env: self.class.rails_env, env_style_env: ENV.fetch("ENV_STYLE_ENV", nil)) ⇒ Environment

Returns a new instance of Environment.



14
15
16
17
18
19
# File 'lib/env_style/environment.rb', line 14

def initialize(config: EnvStyle.config, rails_env: self.class.rails_env,
               env_style_env: ENV.fetch("ENV_STYLE_ENV", nil))
  @config = config
  @rails_env = presence(rails_env)
  @env_style_env = presence(env_style_env)
end

Class Method Details

.rails_envObject

Rails.env as a string when Rails is loaded, otherwise nil.



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

def self.rails_env
  Rails.env.to_s if defined?(Rails) && Rails.respond_to?(:env)
end

Instance Method Details

#colorObject

The tint color for this environment, or nil when it must stay untouched.



32
33
34
35
36
37
# File 'lib/env_style/environment.rb', line 32

def color
  return nil unless @config.enabled
  return nil if name.nil? || name == PRODUCTION

  presence(@config.colors[name]) || presence(@config.default_color)
end

#nameObject

The resolved environment name, or nil when it cannot be determined.



22
23
24
# File 'lib/env_style/environment.rb', line 22

def name
  presence(@config.environment) || @env_style_env || @rails_env
end

#tint?Boolean

Whether the favicon should be tinted for this environment.

Returns:

  • (Boolean)


27
28
29
# File 'lib/env_style/environment.rb', line 27

def tint?
  !color.nil?
end