Class: Lllibrary::Database

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

Overview

Handles connecting to the database, and initializing the schema.

Instance Method Summary collapse

Constructor Details

#initialize(path, adapter) ⇒ Database

Returns a new instance of Database.



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

def initialize(path, adapter)
  @path, @adapter = path, adapter
  connect
end

Instance Method Details

#connectObject



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

def connect
  ActiveRecord::Base.establish_connection(adapter: @adapter, database: @path)
  @connected = true
end

#connected?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/lllibrary/database.rb', line 27

def connected?
  @connected &&= ActiveRecord::Base.connected?
end

#deleteObject



23
24
25
# File 'lib/lllibrary/database.rb', line 23

def delete
  FileUtils.rm @path
end

#disconnectObject



14
15
16
17
# File 'lib/lllibrary/database.rb', line 14

def disconnect
  ActiveRecord::Base.remove_connection
  @connected = false
end

#exists?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/lllibrary/database.rb', line 19

def exists?
  File.exists? @path
end

#generate_schema(&blk) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/lllibrary/database.rb', line 31

def generate_schema(&blk)
  ActiveRecord::Schema.define do
    unless table_exists? :tracks
      create_table :tracks do |t|
        def t.
          # tag info supported by taglib
          string :title
          string :artist
          string :album
          string :genre
          text :comments
          integer :year
          integer :track_number

          # audio properties supported by taglib
          integer :total_time
          integer :bit_rate
          integer :sample_rate
          integer :channels
        end

        def t.
          integer :original_id
          string :title
          string :artist
          string :composer
          string :album
          string :album_artist
          string :genre
          integer :total_time
          integer :disc_number
          integer :disc_count
          integer :track_number
          integer :track_count
          integer :year
          datetime :date_modified, null: false
          datetime :date_added, null: false
          integer :bit_rate
          integer :sample_rate
          text :comments
          integer :play_count, null: false, default: 0
          datetime :play_date
          integer :skip_count, null: false, default: 0
          datetime :skip_date
          integer :rating, null: false, default: 0
        end

        blk.call(t)
      end
    end

    unless table_exists? :playlists
      create_table :playlists do |t|
        t.string :name
        t.datetime :created_at
        t.datetime :updated_at
      end
    end

    unless table_exists? :playlist_items
      create_table :playlist_items do |t|
        t.references :playlist, null: false
        t.references :track, null: false
        t.integer :position
      end
    end
  end
end