Class: Txdb::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/txdb/database.rb

Constant Summary collapse

DEFAULT_HOST =
'127.0.0.1'
DEFAULT_PORT =
'3306'
DEFAULT_POOL =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Database



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/txdb/database.rb', line 12

def initialize(options = {})
  @adapter = options.fetch(:adapter)
  @backend = Txdb::Backends.get(options.fetch(:backend))
  @username = options.fetch(:username)
  @password = options.fetch(:password)
  @host = options.fetch(:host, DEFAULT_HOST)
  @port = options.fetch(:port, DEFAULT_PORT)
  @name = options.fetch(:name)
  @pool = options.fetch(:pool, DEFAULT_POOL)
  @transifex_project = TransifexProject.new(options.fetch(:transifex))
  @connection_string = ConnectionString.new(options).string
  @tables = options.fetch(:tables).map do |table_config|
    Table.new(self, table_config)
  end
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



9
10
11
# File 'lib/txdb/database.rb', line 9

def adapter
  @adapter
end

#backendObject (readonly)

Returns the value of attribute backend.



9
10
11
# File 'lib/txdb/database.rb', line 9

def backend
  @backend
end

#connection_stringObject (readonly)

Returns the value of attribute connection_string.



10
11
12
# File 'lib/txdb/database.rb', line 10

def connection_string
  @connection_string
end

#hostObject (readonly)

Returns the value of attribute host.



9
10
11
# File 'lib/txdb/database.rb', line 9

def host
  @host
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/txdb/database.rb', line 9

def name
  @name
end

#passwordObject (readonly)

Returns the value of attribute password.



9
10
11
# File 'lib/txdb/database.rb', line 9

def password
  @password
end

#poolObject (readonly)

Returns the value of attribute pool.



10
11
12
# File 'lib/txdb/database.rb', line 10

def pool
  @pool
end

#portObject (readonly)

Returns the value of attribute port.



9
10
11
# File 'lib/txdb/database.rb', line 9

def port
  @port
end

#tablesObject (readonly)

Returns the value of attribute tables.



10
11
12
# File 'lib/txdb/database.rb', line 10

def tables
  @tables
end

#transifex_projectObject (readonly)

Returns the value of attribute transifex_project.



10
11
12
# File 'lib/txdb/database.rb', line 10

def transifex_project
  @transifex_project
end

#usernameObject (readonly)

Returns the value of attribute username.



9
10
11
# File 'lib/txdb/database.rb', line 9

def username
  @username
end

Instance Method Details

#connectionObject



28
29
30
# File 'lib/txdb/database.rb', line 28

def connection
  @connection ||= Sequel.connect(connection_string, max_connections: pool)
end

#find_table(name) ⇒ Object



40
41
42
43
# File 'lib/txdb/database.rb', line 40

def find_table(name)
  name = name.to_s
  tables.find { |table| table.name == name }
end

#from(*args, &block) ⇒ Object



32
33
34
# File 'lib/txdb/database.rb', line 32

def from(*args, &block)
  connection.from(*args, &block)
end

#transifex_apiObject



36
37
38
# File 'lib/txdb/database.rb', line 36

def transifex_api
  transifex_project.api
end