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.



37
38
39
40
41
# File 'lib/gemstash/env.rb', line 37

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

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


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

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

.currentObject

Raises:



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

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

.current=(value) ⇒ Object



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

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

.daemonized=(value) ⇒ Object



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

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

.daemonized?Boolean

Returns:

  • (Boolean)


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

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

.migrate(db) ⇒ Object



127
128
129
130
131
# File 'lib/gemstash/env.rb', line 127

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

#base_file(path) ⇒ Object



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

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

#base_pathObject



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

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



133
134
135
# File 'lib/gemstash/env.rb', line 133

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

#cache_clientObject



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/gemstash/env.rb', line 137

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



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

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

#config=(value) ⇒ Object



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

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

#dbObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/gemstash/env.rb', line 105

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

#rackupObject



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

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

#resetObject



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

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