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

Returns a new instance of Database.



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

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)
  @locales = options.fetch(:locales)
  @source_locale = options.fetch(:source_locale)
  @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

#localesObject (readonly)

Returns the value of attribute locales.



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

def locales
  @locales
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

#source_localeObject (readonly)

Returns the value of attribute source_locale.



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

def source_locale
  @source_locale
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



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

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

#find_table(name) ⇒ Object



43
44
45
46
# File 'lib/txdb/database.rb', line 43

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

#from(*args, &block) ⇒ Object



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

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

#transifex_apiObject



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

def transifex_api
  transifex_project.api
end