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.



39
40
41
42
43
# File 'lib/gemstash/env.rb', line 39

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

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/gemstash/env.rb', line 45

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

.currentObject

Raises:



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

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

.current=(value) ⇒ Object



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

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

.daemonized=(value) ⇒ Object



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

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

.daemonized?Boolean

Returns:

  • (Boolean)


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

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

.migrate(db) ⇒ Object



138
139
140
141
142
# File 'lib/gemstash/env.rb', line 138

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



108
109
110
# File 'lib/gemstash/env.rb', line 108

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

#base_file(path) ⇒ Object



96
97
98
# File 'lib/gemstash/env.rb', line 96

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

#base_pathObject



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/gemstash/env.rb', line 84

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



144
145
146
# File 'lib/gemstash/env.rb', line 144

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

#cache_clientObject



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/gemstash/env.rb', line 148

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

#configObject



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

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

#config=(value) ⇒ Object



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

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

#dbObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/gemstash/env.rb', line 116

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://#{URI.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



100
101
102
103
104
105
106
# File 'lib/gemstash/env.rb', line 100

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

#rackupObject



112
113
114
# File 'lib/gemstash/env.rb', line 112

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

#resetObject



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

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