class Tzispa::Environment

Constants

LOCK

Attributes

env[R]

Public Class Methods

[](key) click to toggle source
# File lib/tzispa/environment.rb, line 34
def [](key)
  instance[key]
end
method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/tzispa/environment.rb, line 38
def method_missing(name, *args, &block)
  if instance.respond_to? name
    instance.send name, *args, &block
  elsif instance.key? ekup = name.to_s.upcase
    instance[ekup]
  else
    super
  end
end
new() click to toggle source
# File lib/tzispa/environment.rb, line 22
def initialize
  @env     = Tzispa::Env.new(env: @@opts.delete(:env) || ENV)
  @options = Tzispa::Tzisparc.new(root).options
  @options.merge! @@opts.clone.symbolize!
  LOCK.synchronize { set_env_vars! }
end
opts=(hash) click to toggle source
# File lib/tzispa/environment.rb, line 30
def opts=(hash)
  @@opts = hash.to_h.dup
end
respond_to_missing?(name, _include_priv) click to toggle source
# File lib/tzispa/environment.rb, line 48
def respond_to_missing?(name, _include_priv)
  instance.respond_to?(name) || instance.key?(name.to_s.upcase)
end

Public Instance Methods

[](key) click to toggle source

rubocop:enable Style/ClassVars

# File lib/tzispa/environment.rb, line 54
def [](key)
  @env[key]
end
apps_path() click to toggle source
# File lib/tzispa/environment.rb, line 101
def apps_path
  @options.fetch(:path) do
    case architecture
    when DOMAINS
      DOMAINS_PATH
    when APPLICATION
      APPLICATION_PATH
    end
  end
end
architecture() click to toggle source
# File lib/tzispa/environment.rb, line 94
def architecture
  @options.fetch(:architecture) do
    warn "Tzispa architecture unknown: see `.tzisparc'"
    exit 1
  end
end
bundler_groups() click to toggle source
# File lib/tzispa/environment.rb, line 78
def bundler_groups
  [:default, environment.to_sym]
end
code_reloading?() click to toggle source
# File lib/tzispa/environment.rb, line 70
def code_reloading?
  development?
end
config() click to toggle source
# File lib/tzispa/environment.rb, line 86
def config
  @config ||= root.join(@options.fetch(:config) { DEFAULT_CONFIG })
end
daemonize?() click to toggle source
# File lib/tzispa/environment.rb, line 162
def daemonize?
  @options.key?(:daemonize) && @options.fetch(:daemonize)
end
default_port?() click to toggle source
# File lib/tzispa/environment.rb, line 150
def default_port?
  port == DEFAULT_PORT
end
development?() click to toggle source
# File lib/tzispa/environment.rb, line 66
def development?
  environment == DEVELOPMENT_ENV
end
domains_path() click to toggle source
# File lib/tzispa/environment.rb, line 144
def domains_path
  @domains_path ||= @options.fetch(:domains_path) do
    env[DOMAINS_PATH] || DEFAULT_DOMAINS_PATH
  end
end
environment() click to toggle source
# File lib/tzispa/environment.rb, line 62
def environment
  @environment ||= env[TZISPA_ENV] || rack_env || DEFAULT_ENV
end
environment?(*names) click to toggle source
# File lib/tzispa/environment.rb, line 74
def environment?(*names)
  names.map(&:to_s).include?(environment)
end
host() click to toggle source
# File lib/tzispa/environment.rb, line 112
def host
  @host ||= @options.fetch(:host) do
    env[TZISPA_HOST] || DEFAULT_HOST
  end
end
key?(key) click to toggle source
# File lib/tzispa/environment.rb, line 58
def key?(key)
  @env.key? key
end
port() click to toggle source
# File lib/tzispa/environment.rb, line 124
def port
  @port ||= @options.fetch(:port) do
    env[TZISPA_PORT] || DEFAULT_PORT
  end.to_i
end
project_name() click to toggle source
# File lib/tzispa/environment.rb, line 90
def project_name
  @options.fetch(:project)
end
rackup() click to toggle source
# File lib/tzispa/environment.rb, line 158
def rackup
  root.join(@options.fetch(:rackup) { DEFAULT_RACKUP })
end
root() click to toggle source
# File lib/tzispa/environment.rb, line 82
def root
  @root ||= Pathname.new(Dir.pwd)
end
server_host() click to toggle source
# File lib/tzispa/environment.rb, line 118
def server_host
  @server_host ||= @options.fetch(:server_host) do
    env[TZISPA_SERVER_HOST] || host
  end
end
server_port() click to toggle source
# File lib/tzispa/environment.rb, line 130
def server_port
  @server_port ||= @options.fetch(:server_port) do
    env[TZISPA_SERVER_PORT] || port
  end.to_i
end
ssl?() click to toggle source
# File lib/tzispa/environment.rb, line 154
def ssl?
  env[TZISPA_SSL] == 'yes'
end
to_options() click to toggle source
# File lib/tzispa/environment.rb, line 166
def to_options
  @options.to_h.merge(
    environment: environment,
    apps_path:   apps_path,
    rackup:      rackup,
    host:        server_host,
    port:        server_port
  )
end
uri_port() click to toggle source
# File lib/tzispa/environment.rb, line 136
def uri_port
  if ssl?
    ":#{port}" unless port == 443
  else
    ":#{port}" unless port == 80
  end
end

Private Instance Methods

rack_env() click to toggle source
# File lib/tzispa/environment.rb, line 196
def rack_env
  case env[RACK_ENV]
  when RACK_ENV_DEPLOYMENT
    PRODUCTION_ENV
  else
    env[RACK_ENV]
  end
end
set_application_env_vars!() click to toggle source
# File lib/tzispa/environment.rb, line 191
def set_application_env_vars!
  dotenv = root.join(DEFAULT_DOTENV_ENV % environment)
  env.load!(dotenv) if dotenv.exist?
end
set_env_vars!() click to toggle source
# File lib/tzispa/environment.rb, line 180
def set_env_vars!
  set_application_env_vars!
  set_tzispa_env_vars!
end
set_tzispa_env_vars!() click to toggle source
# File lib/tzispa/environment.rb, line 185
def set_tzispa_env_vars!
  env[TZISPA_ENV]  = env[RACK_ENV] = environment
  env[TZISPA_HOST] = host
  env[TZISPA_PORT] = port.to_s
end