Class: Cellect::Server::Adapters::Postgres

Inherits:
Default
  • Object
show all
Defined in:
lib/cellect/server/adapters/postgres.rb

Instance Method Summary collapse

Methods inherited from Default

#load_project, #load_projects, #project_for

Constructor Details

#initializePostgres

Returns a new instance of Postgres.



8
9
10
11
12
# File 'lib/cellect/server/adapters/postgres.rb', line 8

def initialize
  @pg ||= ConnectionPool.new(size: ENV.fetch('PG_POOL_SIZE', 25).to_i) do
    PG.connect connection_options
  end
end

Instance Method Details

#connection_optionsObject



52
53
54
55
56
57
58
59
60
# File 'lib/cellect/server/adapters/postgres.rb', line 52

def connection_options
  {
    host: ENV.fetch('PG_HOST', '127.0.0.1'),
    port: ENV.fetch('PG_PORT', '5432'),
    dbname: ENV.fetch('PG_DB', 'cellect'),
    user: ENV.fetch('PG_USER', 'cellect'),
    password: ENV.fetch('PG_PASS', '')
  }
end

#load_data_for(project_name) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cellect/server/adapters/postgres.rb', line 28

def load_data_for(project_name)
  with_pg do |pg|
    pg.exec("select id, priority, group_id from project_#{ project_name }_subjects").collect do |row|
      {
        'id' => row['id'].to_i,
        'priority' => row['priority'].to_f,
        'group_id' => row['group_id'].to_i
      }
    end
  end
end

#load_user(project_name, id) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/cellect/server/adapters/postgres.rb', line 40

def load_user(project_name, id)
  with_pg do |pg|
    pg.exec("select subject_id from project_#{ project_name }_classifications where user_id=#{ id }").collect do |row|
      row['subject_id'].to_i
    end
  end
end

#project_listObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cellect/server/adapters/postgres.rb', line 14

def project_list
  with_pg do |pg|
    pg.exec('select * from projects').collect do |row|
      {
        'id' => row['id'].to_i,
        'name' => row['id'],
        'prioritized' => row['prioritized'] == 't',
        'pairwise' => row['pairwise'] == 't',
        'grouped' => row['grouped'] == 't'
      }
    end
  end
end

#with_pgObject



48
49
50
# File 'lib/cellect/server/adapters/postgres.rb', line 48

def with_pg
  @pg.with{ |pg| yield pg }
end