Class: Tzispa::Environment

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/tzispa/environment.rb

Constant Summary collapse

LOCK =
Mutex.new
RACK_ENV =
'RACK_ENV'
TZISPA_ENV =
'TZISPA_ENV'
DEVELOPMENT_ENV =
'development'
DEFAULT_ENV =
'development'
PRODUCTION_ENV =
'deployment'
RACK_ENV_DEPLOYMENT =
'deployment'
DEFAULT_DOTENV_ENV =
'.env.%s'
DEFAULT_CONFIG =
'config'
TZISPA_HOST =
'TZISPA_HOST'
TZISPA_SSL =
'TZISPA_SSL'
TZISPA_SERVER_HOST =
'TZISPA_SERVER_HOST'
DEFAULT_HOST =
'localhost'
TZISPA_PORT =
'TZISPA_PORT'
TZISPA_SERVER_PORT =
'TZISPA_SERVER_PORT'
DEFAULT_PORT =
9412
DEFAULT_RACKUP =
'tzispa.ru'
DEFAULT_ENVIRONMENT_CONFIG =
'environment'
DEFAULT_DOMAINS_PATH =
'apps'
DOMAINS =
'domains'
DOMAINS_PATH =
'apps/%s'
APPLICATION =
'application'
APPLICATION_PATH =
'app'
@@opts =

rubocop:disable Style/ClassVars

{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEnvironment

Returns a new instance of Environment.



65
66
67
68
69
70
# File 'lib/tzispa/environment.rb', line 65

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

Class Method Details

.[](key) ⇒ Object



76
77
78
# File 'lib/tzispa/environment.rb', line 76

def self.[](key)
  instance[key]
end

.development?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/tzispa/environment.rb', line 80

def self.development?
  instance.development?
end

.opts=(hash) ⇒ Object



72
73
74
# File 'lib/tzispa/environment.rb', line 72

def self.opts=(hash)
  @@opts = hash.to_h.dup
end

Instance Method Details

#[](key) ⇒ Object



84
85
86
# File 'lib/tzispa/environment.rb', line 84

def [](key)
  @env[key]
end

#apps_pathObject



123
124
125
126
127
128
129
130
131
132
# File 'lib/tzispa/environment.rb', line 123

def apps_path
  @options.fetch(:path) do
    case architecture
    when DOMAINS
      DOMAINS_PATH
    when APPLICATION
      APPLICATION_PATH
    end
  end
end

#architectureObject



112
113
114
115
116
117
# File 'lib/tzispa/environment.rb', line 112

def architecture
  @options.fetch(:architecture) do
    puts "Tzispa architecture unknown: see `.tzisparc'"
    exit 1
  end
end

#bundler_groupsObject



104
105
106
# File 'lib/tzispa/environment.rb', line 104

def bundler_groups
  [:default, environment.to_sym]
end

#code_reloading?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/tzispa/environment.rb', line 96

def code_reloading?
  development?
end

#configObject



134
135
136
# File 'lib/tzispa/environment.rb', line 134

def config
  @config ||= root.join(@options.fetch(:config) { DEFAULT_CONFIG })
end

#daemonize?Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/tzispa/environment.rb', line 188

def daemonize?
  @options.key?(:daemonize) && @options.fetch(:daemonize)
end

#default_port?Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/tzispa/environment.rb', line 176

def default_port?
  port == DEFAULT_PORT
end

#development?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/tzispa/environment.rb', line 92

def development?
  environment == DEVELOPMENT_ENV
end

#domains_pathObject



170
171
172
173
174
# File 'lib/tzispa/environment.rb', line 170

def domains_path
  @domains_path ||= @options.fetch(:domains_path) do
    env[DOMAINS_PATH] || DEFAULT_DOMAINS_PATH
  end
end

#environmentObject



88
89
90
# File 'lib/tzispa/environment.rb', line 88

def environment
  @environment ||= env[TZISPA_ENV] || rack_env || DEFAULT_ENV
end

#environment?(*names) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/tzispa/environment.rb', line 100

def environment?(*names)
  names.map(&:to_s).include?(environment)
end

#hostObject



138
139
140
141
142
# File 'lib/tzispa/environment.rb', line 138

def host
  @host ||= @options.fetch(:host) do
    env[TZISPA_HOST] || DEFAULT_HOST
  end
end

#portObject



150
151
152
153
154
# File 'lib/tzispa/environment.rb', line 150

def port
  @port ||= @options.fetch(:port) do
    env[TZISPA_PORT] || DEFAULT_PORT
  end.to_i
end

#project_nameObject



108
109
110
# File 'lib/tzispa/environment.rb', line 108

def project_name
  @options.fetch(:project)
end

#rackupObject



184
185
186
# File 'lib/tzispa/environment.rb', line 184

def rackup
  root.join(@options.fetch(:rackup) { DEFAULT_RACKUP })
end

#rootObject



119
120
121
# File 'lib/tzispa/environment.rb', line 119

def root
  @root ||= Pathname.new(Dir.pwd)
end

#server_hostObject



144
145
146
147
148
# File 'lib/tzispa/environment.rb', line 144

def server_host
  @server_host ||= @options.fetch(:server_host) do
    env[TZISPA_SERVER_HOST] || host
  end
end

#server_portObject



156
157
158
159
160
# File 'lib/tzispa/environment.rb', line 156

def server_port
  @server_port ||= @options.fetch(:server_port) do
    env[TZISPA_SERVER_PORT] || port
  end.to_i
end

#ssl?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/tzispa/environment.rb', line 180

def ssl?
  env[TZISPA_SSL] == 'yes'
end

#to_optionsObject



192
193
194
195
196
197
198
199
200
# File 'lib/tzispa/environment.rb', line 192

def to_options
  @options.to_h.merge(
    environment: environment,
    apps_path:   apps_path,
    rackup:      rackup,
    host:        server_host,
    port:        server_port
  )
end

#uri_portObject



162
163
164
165
166
167
168
# File 'lib/tzispa/environment.rb', line 162

def uri_port
  if ssl?
    ":#{port}" unless port == 443
  else
    ":#{port}" unless port == 80
  end
end