class Kingfisher::DatabaseBackends::SqlLite3

Attributes

database_url[R]
db[R]

Public Class Methods

new(database_url:) click to toggle source
# File lib/kingfisher/database_backends/sqlite3.rb, line 6
def initialize(database_url:)
  @database_url = database_url
  @db = open_db
end

Public Instance Methods

all(model) click to toggle source
# File lib/kingfisher/database_backends/sqlite3.rb, line 11
def all(model)
  db[table_name(model)].all
end
create(model, params) click to toggle source
# File lib/kingfisher/database_backends/sqlite3.rb, line 15
def create(model, params)
  id = db[table_name(model)].insert(params)
  attributes = symbolize_keys(params).merge(id: id)
  model.new(attributes)
end
find(model, id) click to toggle source
# File lib/kingfisher/database_backends/sqlite3.rb, line 21
def find(model, id)
  find_by(model, id: id)
end
find_by(model, attributes) click to toggle source
# File lib/kingfisher/database_backends/sqlite3.rb, line 25
def find_by(model, attributes)
  db[table_name(model)][**attributes]
end

Private Instance Methods

open_db() click to toggle source
# File lib/kingfisher/database_backends/sqlite3.rb, line 32
def open_db
  Sequel.connect database_url
end
symbolize_keys(hash) click to toggle source
# File lib/kingfisher/database_backends/sqlite3.rb, line 40
def symbolize_keys(hash)
  hash.each_with_object({}) do |(key, value), new_hash|
    new_hash[key.to_sym] = value
  end
end
table_name(model) click to toggle source
# File lib/kingfisher/database_backends/sqlite3.rb, line 36
def table_name(model)
  model.name.downcase.to_sym
end