class AudioBookCreator::PageDb

a name value store stored in sqlite this is used for pages and also settings

Attributes

encode[RW]
filename[RW]
table_name[RW]

Public Class Methods

new(filename, table_name, encode) click to toggle source
# File lib/audio_book_creator/page_db.rb, line 12
def initialize(filename, table_name, encode)
  @filename = filename
  @table_name = table_name
  @encode = encode
end

Public Instance Methods

[](key) click to toggle source
# File lib/audio_book_creator/page_db.rb, line 23
def [](key)
  value = db.execute("select contents from #{table_name} where name = ?", key).map { |row| row.first }.first
  encode && value ? JSON.parse(value, :symbolize_names => true) : value
end
[]=(key, value) click to toggle source
# File lib/audio_book_creator/page_db.rb, line 18
def []=(key, value)
  value = JSON.generate(value) if encode && value
  db.execute "insert into #{table_name} (name, contents) values (?, ?)", [key, value]
end
each(&block) click to toggle source
# File lib/audio_book_creator/page_db.rb, line 32
def each(&block)
  db.execute "select name, contents from #{table_name}", &block
end
include?(key) click to toggle source
# File lib/audio_book_creator/page_db.rb, line 28
def include?(key)
  self[key]
end

Private Instance Methods

create() click to toggle source
# File lib/audio_book_creator/page_db.rb, line 42
def create
  SQLite3::Database.new(filename).tap do |db|
    db.execute("create table if not exists #{table_name} (name text, contents blob)")
  end
end
db() click to toggle source
# File lib/audio_book_creator/page_db.rb, line 38
def db
  @db ||= create
end