Class: AudioBookCreator::PageDb

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/audio_book_creator/page_db.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, table_name, encode) ⇒ PageDb

Returns a new instance of PageDb.



12
13
14
15
16
# 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

Instance Attribute Details

#encodeObject

Returns the value of attribute encode.



10
11
12
# File 'lib/audio_book_creator/page_db.rb', line 10

def encode
  @encode
end

#filenameObject

Returns the value of attribute filename.



10
11
12
# File 'lib/audio_book_creator/page_db.rb', line 10

def filename
  @filename
end

#table_nameObject

Returns the value of attribute table_name.



10
11
12
# File 'lib/audio_book_creator/page_db.rb', line 10

def table_name
  @table_name
end

Instance Method Details

#[](key) ⇒ Object



23
24
25
26
# 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) ⇒ Object



18
19
20
21
# 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) ⇒ Object



32
33
34
# 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) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/audio_book_creator/page_db.rb', line 28

def include?(key)
  self[key]
end