Class: Baza::Database

Inherits:
Object
  • Object
show all
Includes:
DatabaseModelFunctionality
Defined in:
lib/baza/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DatabaseModelFunctionality

#model_name, #to_model

Constructor Details

#initialize(args) ⇒ Database

Returns a new instance of Database.



6
7
8
9
10
11
12
# File 'lib/baza/database.rb', line 6

def initialize(args)
  @changes = {}
  @db = args.fetch(:db)
  @driver = args.fetch(:driver)
  @name = args.fetch(:name)
  @name_was = @name
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



4
5
6
# File 'lib/baza/database.rb', line 4

def db
  @db
end

#driverObject (readonly)

Returns the value of attribute driver.



4
5
6
# File 'lib/baza/database.rb', line 4

def driver
  @driver
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/baza/database.rb', line 4

def name
  @name
end

Instance Method Details

#import_file!(path, args = {}) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/baza/database.rb', line 14

def import_file!(path, args = {})
  File.open(path, "r") do |io|
    use do
      Baza::Commands::Importer.new({db: @db, io: io}.merge(args)).execute
    end
  end
end

#name_changed?Boolean

Returns:

  • (Boolean)


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

def name_changed?
  @changes.key?(:name) && @changes.fetch(:name).to_s != name.to_s
end

#name_wasObject



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

def name_was
  @name_was
end

#save!Object



60
61
62
# File 'lib/baza/database.rb', line 60

def save!
  raise Baza::Errors::NotImplemented
end

#table(name) ⇒ Object



47
48
49
50
51
# File 'lib/baza/database.rb', line 47

def table(name)
  table = tables(name: name).first
  raise Baza::Errors::TableNotFound unless table
  table
end

#table_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
# File 'lib/baza/database.rb', line 53

def table_exists?(name)
  table(name)
  true
rescue Baza::Errors::TableNotFound
  false
end

#tables(args = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/baza/database.rb', line 36

def tables(args = {})
  list_args = {database: name}
  list_args[:name] = args.fetch(:name) if args[:name]

  ArrayEnumerator.new do |yielder|
    @db.tables.list(list_args) do |table|
      yielder << table
    end
  end
end

#to_paramObject



64
65
66
# File 'lib/baza/database.rb', line 64

def to_param
  name
end

#use(&blk) ⇒ Object



68
69
70
# File 'lib/baza/database.rb', line 68

def use(&blk)
  @db.databases.with_database(name, &blk)
end