Class: Gemstash::Env

Inherits:
Object
  • Object
show all
Defined in:
lib/gemstash/env.rb

Overview

Storage for application-wide variables and configuration.

Defined Under Namespace

Modules: Helper Classes: EnvNotSetError, RackMiddleware

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil, cache: nil, db: nil) ⇒ Env

Returns a new instance of Env.



42
43
44
45
46
# File 'lib/gemstash/env.rb', line 42

def initialize(config = nil, cache: nil, db: nil)
  @config = config
  @cache = cache
  @db = db
end

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/gemstash/env.rb', line 48

def self.available?
  !Thread.current[:gemstash_env].nil?
end

.currentObject

Raises:



52
53
54
55
56
# File 'lib/gemstash/env.rb', line 52

def self.current
  raise EnvNotSetError unless Thread.current[:gemstash_env]

  Thread.current[:gemstash_env]
end

.current=(value) ⇒ Object



58
59
60
# File 'lib/gemstash/env.rb', line 58

def self.current=(value)
  Thread.current[:gemstash_env] = value
end

.daemonized=(value) ⇒ Object



68
69
70
71
# File 'lib/gemstash/env.rb', line 68

def self.daemonized=(value)
  value = false if value.nil?
  @daemonized = value
end

.daemonized?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
# File 'lib/gemstash/env.rb', line 62

def self.daemonized?
  raise "Daemonized hasn't been set yet!" if @daemonized.nil?

  @daemonized
end

.migrate(db) ⇒ Object



156
157
158
159
160
# File 'lib/gemstash/env.rb', line 156

def self.migrate(db)
  Sequel.extension :migration
  migrations_dir = File.expand_path("migrations", __dir__)
  Sequel::Migrator.run(db, migrations_dir, :use_transactions => true)
end

Instance Method Details

#atomic_write(file, &block) ⇒ Object



126
127
128
# File 'lib/gemstash/env.rb', line 126

def atomic_write(file, &block)
  File.atomic_write(file, File.dirname(file), &block)
end

#base_file(path) ⇒ Object



101
102
103
# File 'lib/gemstash/env.rb', line 101

def base_file(path)
  File.join(base_path, path)
end

#base_pathObject



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/gemstash/env.rb', line 89

def base_path
  dir = config[:base_path]

  if config.default?(:base_path)
    FileUtils.mkpath(dir) unless Dir.exist?(dir)
  else
    raise "Base path '#{dir}' is not writable" unless File.writable?(dir)
  end

  dir
end

#cacheObject



162
163
164
# File 'lib/gemstash/env.rb', line 162

def cache
  @cache ||= Gemstash::Cache.new(cache_client)
end

#cache_clientObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/gemstash/env.rb', line 166

def cache_client
  @cache_client ||= begin
    case config[:cache_type]
    when "memory"
      Gemstash::LruReduxClient.new
    when "memcached"
      Dalli::Client.new(config[:memcached_servers])
    when "redis"
      Gemstash::RedisClient.new(config[:redis_servers])
    else
      raise "Invalid cache client: '#{config[:cache_type]}'"
    end
  end
end

#configObject



73
74
75
# File 'lib/gemstash/env.rb', line 73

def config
  @config ||= Gemstash::Configuration.new
end

#config=(value) ⇒ Object



77
78
79
80
# File 'lib/gemstash/env.rb', line 77

def config=(value)
  reset
  @config = value
end

#dbObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/gemstash/env.rb', line 134

def db
  @db ||= begin
    case config[:db_adapter]
    when "sqlite3"
      db_path = base_file("gemstash.db")

      db = if RUBY_PLATFORM == "java"
        Sequel.connect("jdbc:sqlite:#{db_path}", config.database_connection_config)
      else
        Sequel.connect("sqlite://#{CGI.escape(db_path)}", config.database_connection_config)
      end
    when "postgres", "mysql", "mysql2"
      db = Sequel.connect(config[:db_url], config.database_connection_config)
    else
      raise "Unsupported DB adapter: '#{config[:db_adapter]}'"
    end

    Gemstash::Env.migrate(db)
    db
  end
end

#log_fileObject



105
106
107
108
109
110
111
# File 'lib/gemstash/env.rb', line 105

def log_file
  if config[:log_file] == :stdout
    $stdout
  else
    base_file(config[:log_file] || "server.log")
  end
end

#pidfileObject



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/gemstash/env.rb', line 113

def pidfile
  if config[:pidfile]
    pathname = Pathname.new(config[:pidfile])
    if pathname.relative?
      base_file(pathname.to_s)
    else
      pathname.to_s
    end
  else
    base_file("puma.pid")
  end
end

#rackupObject



130
131
132
# File 'lib/gemstash/env.rb', line 130

def rackup
  File.expand_path("config.ru", __dir__)
end

#resetObject



82
83
84
85
86
87
# File 'lib/gemstash/env.rb', line 82

def reset
  @config = nil
  @cache = nil
  @cache_client = nil
  @db = nil
end