Class: Shakapacker::Env

Inherits:
Object
  • Object
show all
Defined in:
sig/shakapacker/env.rbs,
lib/shakapacker/env.rb

Overview

Environment inquiry for Shakapacker

Constant Summary collapse

FALLBACK_ENV =
"production".freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance) ⇒ Env

Creates a new env instance

Parameters:



7
8
9
# File 'sig/shakapacker/env.rbs', line 7

def initialize(instance)
  @instance = instance
end

Class Method Details

.inquire(instance) ⇒ ActiveSupport::StringInquirer

Returns the current Rails environment as an ActiveSupport::StringInquirer

Parameters:

Returns:

  • (ActiveSupport::StringInquirer)


4
5
6
# File 'sig/shakapacker/env.rbs', line 4

def self.inquire(instance)
  new(instance).inquire
end

Instance Method Details

#available_environmentsArray[String], Hash[String, untyped]

Returns:

  • (Array[String], Hash[String, untyped])


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/shakapacker/env.rb', line 42

def available_environments
  if config_path.exist?
    begin
      YAML.load_file(config_path.to_s, aliases: true)
    rescue ArgumentError
      YAML.load_file(config_path.to_s)
    end
  else
    [].freeze
  end
rescue Psych::SyntaxError => e
  raise "YAML syntax error occurred while parsing #{config_path}. " \
        "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
        "Error: #{e.message}"
end

#config_pathPathname

Returns the config path

Returns:

  • (Pathname)


13
# File 'sig/shakapacker/env.rbs', line 13

def config_path: () -> Pathname

#currentActiveSupport::StringInquirer?

Returns:

  • (ActiveSupport::StringInquirer, nil)


20
21
22
23
24
25
26
27
# File 'lib/shakapacker/env.rb', line 20

def current
  env = if defined?(Rails) && Rails.respond_to?(:env)
    Rails.env
  else
    ENV["RAILS_ENV"].presence || ENV["RACK_ENV"].presence || Shakapacker::DEFAULT_ENV
  end
  env.presence_in(available_environments)
end

#fallback_env_warningvoid

This method returns an undefined value.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/shakapacker/env.rb', line 29

def fallback_env_warning
  env_value = if defined?(Rails) && Rails.respond_to?(:env)
    Rails.env
  else
    ENV["RAILS_ENV"].presence || ENV["RACK_ENV"].presence || Shakapacker::DEFAULT_ENV
  end
  logger.info "RAILS_ENV=#{env_value} environment is not defined in #{config_path}, falling back to #{FALLBACK_ENV} environment"
rescue NameError, NoMethodError
  # Logger may not be fully functional without Rails (e.g., ActiveSupport::IsolatedExecutionState
  # is not available). Fall back to puts, matching Configuration#log_fallback.
  puts "RAILS_ENV=#{env_value} environment is not defined in #{config_path}, falling back to #{FALLBACK_ENV} environment"
end

#inquireActiveSupport::StringInquirer

Returns the environment inquiry

Returns:

  • (ActiveSupport::StringInquirer)


10
11
12
13
# File 'sig/shakapacker/env.rbs', line 10

def inquire
  fallback_env_warning if config_path.exist? && !current
  (current || FALLBACK_ENV).inquiry
end

#loggerActiveSupport::TaggedLogging

Returns the logger

Returns:

  • (ActiveSupport::TaggedLogging)


16
# File 'sig/shakapacker/env.rbs', line 16

def logger: () -> ActiveSupport::TaggedLogging