Class: Gemstash::Env
- Inherits:
-
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.
38
39
40
41
42
|
# File 'lib/gemstash/env.rb', line 38
def initialize(config = nil, cache: nil, db: nil)
@config = config
@cache = cache
@db = db
end
|
Class Method Details
.available? ⇒ Boolean
44
45
46
|
# File 'lib/gemstash/env.rb', line 44
def self.available?
!Thread.current[:gemstash_env].nil?
end
|
.current ⇒ Object
48
49
50
51
|
# File 'lib/gemstash/env.rb', line 48
def self.current
raise EnvNotSetError unless Thread.current[:gemstash_env]
Thread.current[:gemstash_env]
end
|
.current=(value) ⇒ Object
53
54
55
|
# File 'lib/gemstash/env.rb', line 53
def self.current=(value)
Thread.current[:gemstash_env] = value
end
|
.daemonized=(value) ⇒ Object
65
66
67
68
|
# File 'lib/gemstash/env.rb', line 65
def self.daemonized=(value)
value = false if value.nil?
@daemonized = value
end
|
.daemonized? ⇒ Boolean
57
58
59
60
61
62
63
|
# File 'lib/gemstash/env.rb', line 57
def self.daemonized?
if @daemonized.nil?
raise "Daemonized hasn't been set yet!"
else
@daemonized
end
end
|
.migrate(db) ⇒ Object
132
133
134
135
136
|
# File 'lib/gemstash/env.rb', line 132
def self.migrate(db)
Sequel.extension :migration
migrations_dir = File.expand_path("../migrations", __FILE__)
Sequel::Migrator.run(db, migrations_dir, :use_transactions => true)
end
|
Instance Method Details
#atomic_write(file, &block) ⇒ Object
102
103
104
|
# File 'lib/gemstash/env.rb', line 102
def atomic_write(file, &block)
File.atomic_write(file, File.dirname(file), &block)
end
|
#base_file(path) ⇒ Object
98
99
100
|
# File 'lib/gemstash/env.rb', line 98
def base_file(path)
File.join(base_path, path)
end
|
#base_path ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/gemstash/env.rb', line 86
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
|
#cache ⇒ Object
138
139
140
|
# File 'lib/gemstash/env.rb', line 138
def cache
@cache ||= Gemstash::Cache.new(cache_client)
end
|
#cache_client ⇒ Object
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/gemstash/env.rb', line 142
def cache_client
@cache_client ||= begin
case config[:cache_type]
when "memory"
Gemstash::LruReduxClient.new
when "memcached"
Dalli::Client.new(config[:memcached_servers])
else
raise "Invalid cache client: '#{config[:cache_type]}'"
end
end
end
|
#config=(value) ⇒ Object
74
75
76
77
|
# File 'lib/gemstash/env.rb', line 74
def config=(value)
reset
@config = value
end
|
#db ⇒ Object
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/gemstash/env.rb', line 110
def db
@db ||= begin
case config[:db_adapter]
when "sqlite3"
db_path = base_file("gemstash.db")
if RUBY_PLATFORM == "java"
db = Sequel.connect("jdbc:sqlite:#{db_path}", max_connections: 1)
else
db = Sequel.connect("sqlite://#{URI.escape(db_path)}", max_connections: 1)
end
when "postgres", "mysql"
db = Sequel.connect(config[:db_url])
else
raise "Unsupported DB adapter: '#{config[:db_adapter]}'"
end
Gemstash::Env.migrate(db)
db
end
end
|
#rackup ⇒ Object
106
107
108
|
# File 'lib/gemstash/env.rb', line 106
def rackup
File.expand_path("../config.ru", __FILE__)
end
|
#reset ⇒ Object
79
80
81
82
83
84
|
# File 'lib/gemstash/env.rb', line 79
def reset
@config = nil
@cache = nil
@cache_client = nil
@db = nil
end
|