Class: Annotations2triannon::RevsDb

Inherits:
Object
  • Object
show all
Defined in:
lib/annotations2triannon/revs_db.rb

Constant Summary collapse

@@log =
Logger.new('log/revs_db.log')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRevsDb

Returns a new instance of RevsDb.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/annotations2triannon/revs_db.rb', line 22

def initialize
  @db_config = {}
  @db_config['host'] = ENV['REVS_DB_HOST'] || 'localhost'
  @db_config['port'] = ENV['REVS_DB_PORT'] || '3306'
  @db_config['user'] = ENV['REVS_DB_USER'] || 'revs'
  @db_config['password'] = ENV['REVS_DB_PASS'] || ''
  @db_config['database'] = ENV['REVS_DB_DATABASE'] || 'revs'
  options = @db_config.merge(
      {
          :encoding => 'utf8',
          :max_connections => 10,
          :logger => @@log
      })
  @db = Sequel.mysql2(options)
  @db.extension(:pagination)
  # Ensure the connection is good on startup, raises exceptions on failure
  @@log.info "#{@db} connected: #{@db.test_connection}"
end

Instance Attribute Details

#dbObject

Returns the value of attribute db.



15
16
17
# File 'lib/annotations2triannon/revs_db.rb', line 15

def db
  @db
end

#db_configObject

Returns the value of attribute db_config.



16
17
18
# File 'lib/annotations2triannon/revs_db.rb', line 16

def db_config
  @db_config
end

Class Method Details

.log_model_info(m) ⇒ Object



18
19
20
# File 'lib/annotations2triannon/revs_db.rb', line 18

def self.log_model_info(m)
  @@log.info "table: #{m.table_name}, columns: #{m.columns}, pk: #{m.primary_key}"
end

Instance Method Details

#annotation(id) ⇒ Object



41
42
43
# File 'lib/annotations2triannon/revs_db.rb', line 41

def annotation(id)
  @db[:annotations][:id => id]
end

#annotation_idsObject



49
50
51
# File 'lib/annotations2triannon/revs_db.rb', line 49

def annotation_ids
  @db[:annotations].order(:user_id).select(:user_id, :id)
end

#annotationsObject



45
46
47
# File 'lib/annotations2triannon/revs_db.rb', line 45

def annotations
  @db[:annotations]
end

#annotations_join_usersObject



53
54
55
56
# File 'lib/annotations2triannon/revs_db.rb', line 53

def annotations_join_users
  @db[:annotations].join_table(:inner, @db[:users], :id=>:user_id)
  # @db[:annotations].join_table(:outer, @db[:users], :id=>:user_id)
end

#user(id) ⇒ Object



62
63
64
# File 'lib/annotations2triannon/revs_db.rb', line 62

def user(id)
  @db[:users][:id => id]
end

#usersObject



58
59
60
# File 'lib/annotations2triannon/revs_db.rb', line 58

def users
  @db[:users]
end