rails_env_local 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 485eb26768f46bc69496783af8234edc0982b99a
4
- data.tar.gz: 338f1c9c6259986f9a5c204475b08f3f1186192a
3
+ metadata.gz: a68badb555149e69a20b383d3e7be6fb11e8d453
4
+ data.tar.gz: 8e2e0d41b09a066795e6939fa728cd079b868414
5
5
  SHA512:
6
- metadata.gz: 686176257b35842b362bdc9071d9c95dfabbaa29b25c704c953add7afb40c765545aa61d9b2af1ffde0b9e423de28e221532f31fd6301b3ec776f8bfce1d6c90
7
- data.tar.gz: 5ca9d515a397bbcc2cb535b682e4c42c361d8b3c69e7e4d38649f633dd7e1e09d336f02fbae1646732e16d4bb5d2a887227a846264e86aadbf7f59b0d9aa280f
6
+ metadata.gz: 5281df61c9259be336e531c4becef9796d3d7aa94771f85482c994fb197f4c76e9a927c72565760293abe551d5cff716fc9f3733464d8a3d9d0e6756e0e3c426
7
+ data.tar.gz: 527bcae1a18bd23388ad048d695ab7655f2df3b9a68b2b79ee3dba74a032fb27ad1c49e780f72773698a9eb56036dcf27746bd89baa5fcefeebb85350d1b5f79
data/README.md CHANGED
@@ -111,6 +111,15 @@ A: Yeah, pretty much. Name your deployed environment "dev", "develop", or "anyt
111
111
  RailsEnvLocal.set_local_environment(set_rails_env: false)
112
112
  ```
113
113
 
114
+ #### `force`
115
+ * Type: boolean
116
+ * Default: `false`
117
+ * Effect: Setting `Rails.env`, no matter what, and `RAILS_ENV` / `RACK_ENV` variables according to other options given.
118
+ * Example:
119
+ ```ruby
120
+ RailsEnvLocal.set_local_environment(force: true)
121
+ ```
122
+
114
123
  ## Development
115
124
 
116
125
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run a single suite of tests, or run `wwtd` to run all specs like TravisCI would. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -1,5 +1,6 @@
1
1
  require "rails_env_local/version"
2
2
  require "rails"
3
+ require "rails_env_local/rails_env"
3
4
 
4
5
  # Usage:
5
6
  #
@@ -33,6 +34,10 @@ require "rails"
33
34
  # set_rails_env: true or false (default is true)
34
35
  # Effect: will additionally set RACK_ENV to match Rails.env
35
36
  # Example: RailsEnvLocal.set_local_environment(set_rails_env: false)
37
+ #
38
+ # force: true or false (default is false)
39
+ # Effect: will set Rails.env to environment no matter what.
40
+ # Example: RailsEnvLocal.set_local_environment(force: true)
36
41
 
37
42
  module RailsEnvLocal
38
43
  ORIGINAL_RAILS_DEVELOPMENT_ENVIRONMENT = "development"
@@ -41,12 +46,31 @@ module RailsEnvLocal
41
46
  # the environment name. For one example of this see: https://github.com/bkeepers/dotenv#multiple-rails-environments
42
47
  ALTERNATE_RAILS_DEVELOPMENT_ENVIRONMENT = "localdev"
43
48
  def self.set_local_environment(environment: ALTERNATE_RAILS_DEVELOPMENT_ENVIRONMENT, **options)
44
- if ENV["RAILS_ENV"] == ORIGINAL_RAILS_DEVELOPMENT_ENVIRONMENT || Rails.env == ORIGINAL_RAILS_DEVELOPMENT_ENVIRONMENT
49
+ options = {set_rails_env: true, set_rack_env: true, verbose: false, force: false}.merge(options)
50
+ if was_development? || was_default? || options[:force]
45
51
  Rails.env = environment
46
- options = {set_rails_env: true, set_rack_env: true, verbose: false}.merge(options)
47
52
  ENV["RAILS_ENV"] = Rails.env if options[:set_rails_env]
48
53
  ENV["RACK_ENV"] = Rails.env if options[:set_rack_env]
49
54
  puts "switching to custom local development environment: #{environment}" if options[:verbose]
55
+ elsif [ENV["RAILS_ENV"], ENV["RACK_ENV"], Rails.env].compact.uniq.length > 1
56
+ msg = %(ENV["RAILS_ENV"], ENV["RACK_ENV"] and Rails.env are out of sync) if options[:verbose]
57
+ warn(msg)
58
+ Rails.logger.error(msg) if Rails.logger
50
59
  end
51
60
  end
61
+
62
+ private
63
+ def self.environment_variables
64
+ [ENV["RAILS_ENV"], ENV["RACK_ENV"]]
65
+ end
66
+
67
+ def self.was_development?
68
+ environment_variables.include?(ORIGINAL_RAILS_DEVELOPMENT_ENVIRONMENT)
69
+ end
70
+
71
+ def self.was_default?
72
+ environment_variables.compact.empty?
73
+ end
52
74
  end
75
+
76
+ Rails.singleton_class.send(:prepend, RailsEnvLocal::RailsEnv)
@@ -0,0 +1,14 @@
1
+ module RailsEnvLocal
2
+ # This module is prepended to Rails module
3
+ module RailsEnv
4
+ def env
5
+ result = super
6
+ if result == "development"
7
+ msg = %[WARNING: environment is incorrectly "development". Early access of Rails.env might be the cause, consider moving `RailsEnvLocal.set_local_environment` up in config/boot.rb]
8
+ warn(msg)
9
+ Rails.logger.error(msg) if Rails.logger
10
+ end
11
+ result
12
+ end
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module RailsEnvLocal
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_env_local
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Boling
@@ -123,6 +123,7 @@ files:
123
123
  - gemfiles/rails_5_0.gemfile
124
124
  - gemfiles/rails_5_0.gemfile.lock
125
125
  - lib/rails_env_local.rb
126
+ - lib/rails_env_local/rails_env.rb
126
127
  - lib/rails_env_local/version.rb
127
128
  - rails_env_local.gemspec
128
129
  homepage: https://github.com/pboling/rails_env_local