lib/tynn/environment.rb in tynn-1.4.0 vs lib/tynn/environment.rb in tynn-2.0.0.alpha
- old
+ new
@@ -1,92 +1,118 @@
+# frozen_string_literal: true
+
class Tynn
- # Public: Adds helper methods to get and check the current environment.
+ # Adds helper methods to get and check the current environment.
+ # By default, the environment is based on <tt>ENV["RACK_ENV"]</tt>.
#
- # Examples
- #
# require "tynn"
# require "tynn/environment"
#
# Tynn.plugin(Tynn::Environment)
#
+ # # Accessing the current environment.
# Tynn.environment # => :development
#
+ # # Checking the current environment.
# Tynn.development? # => true
# Tynn.production? # => false
# Tynn.test? # => false
+ # Tynn.staging? # => false
#
- # By default, the environment is based on <tt>ENV["RACK_ENV"]</tt>.
+ # # Changing the current environment.
+ # Tynn.set(:environment, :test)
#
- # Examples
+ # # Performing operations in specific environments.
+ # Tynn.configure(:development, :test) do
+ # # ...
+ # end
#
- # Tynn.plugin(Tynn::Environment, env: ENV["RACK_ENV"])
- #
module Environment
- # Internal: Configures current environment.
- def self.setup(app, env: ENV["RACK_ENV"])
+ def self.setup(app, env: ENV["RACK_ENV"]) # :nodoc:
app.set(:environment, (env || :development).to_sym)
end
module ClassMethods
- # Public: Returns current environment.
+ # Yields if current environment matches one of the given environments.
#
- # Examples
+ # class MyApp < Tynn
+ # configure(:development, :staging) do
+ # use(BetterErrors::Middleware)
+ # end
#
+ # configure(:production) do
+ # plugin(Tynn::SSL)
+ # end
+ # end
+ #
+ def configure(*envs)
+ yield(self) if envs.include?(environment)
+ end
+
+ # Returns the current environment for the application.
+ #
# Tynn.environment
# # => :development
#
- # Tynn.set(:environment, :test)
+ # Tynn.set(environment, :test)
#
# Tynn.environment
# # => :test
#
def environment
- return settings[:environment]
+ settings[:environment]
end
- # Public: Returns +true+ if the current environment is +:development+.
- # Otherwise returns +false+.
+ # Checks if current environment is development. Returns <tt>true</tt> if
+ # <tt>environment</tt> is <tt>:development</tt>. Otherwise, <tt>false</tt>.
#
- # Examples
- #
# Tynn.set(:environment, :test)
# Tynn.development? # => false
#
# Tynn.set(:environment, :development)
# Tynn.development? # => true
#
def development?
- return environment == :development
+ environment == :development
end
- # Public: Returns +true+ if the current environment is +:test+.
- # Otherwise returns +false+.
+ # Checks if current environment is test. Returns <tt>true</tt> if
+ # <tt>environment</tt> is <tt>:test</tt>. Otherwise, <tt>false</tt>.
#
- # Examples
- #
# Tynn.set(:environment, :development)
# Tynn.test? # => false
#
# Tynn.set(:environment, :test)
# Tynn.test? # => true
#
def test?
- return environment == :test
+ environment == :test
end
- # Public: Returns +true+ if the current environment is +:production+.
- # Otherwise returns +false+.
+ # Checks if current environment is production. Returns <tt>true</tt> if
+ # <tt>environment</tt> is <tt>:production</tt>. Otherwise, <tt>false</tt>.
#
- # Examples
- #
# Tynn.set(:environment, :development)
# Tynn.production? # => false
#
# Tynn.set(:environment, :production)
# Tynn.production? # => true
#
def production?
- return environment == :production
+ environment == :production
+ end
+
+ # Checks if current environment is staging. Returns <tt>true</tt> if
+ # <tt>environment</tt> is <tt>:staging</tt>. Otherwise, <tt>false</tt>.
+ #
+ # Tynn.set(environment, :test)
+ # Tynn.staging? # => false
+ #
+ # Tynn.set(:environment, :staging)
+ # Tynn.staging? # => true
+ #
+ def staging?
+ environment == :staging
end
end
end
end