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

.migrate(db) ⇒ Object



145
146
147
148
149
# File 'lib/gemstash/env.rb', line 145

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



115
116
117
# File 'lib/gemstash/env.rb', line 115

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

#base_file(path) ⇒ Object



90
91
92
# File 'lib/gemstash/env.rb', line 90

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

#base_pathObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/gemstash/env.rb', line 78

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



151
152
153
# File 'lib/gemstash/env.rb', line 151

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

#cache_clientObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/gemstash/env.rb', line 155

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



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

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

#config=(value) ⇒ Object



66
67
68
69
# File 'lib/gemstash/env.rb', line 66

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

#dbObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/gemstash/env.rb', line 123

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



94
95
96
97
98
99
100
# File 'lib/gemstash/env.rb', line 94

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

#pidfileObject



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/gemstash/env.rb', line 102

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



119
120
121
# File 'lib/gemstash/env.rb', line 119

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

#resetObject



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

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