Last active
December 26, 2015 21:39
-
-
Save bwinterling/7217477 to your computer and use it in GitHub Desktop.
Sequel Merchant.rb - we replaced the YAML database with a sqlite3 database.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler' | |
# require 'pry' | |
require 'csv' | |
require './lib/sql_items' | |
Bundler.require | |
class MerchantSequel | |
attr_reader :database, :merchants_table | |
def initialize | |
@database ||= Sequel.sqlite('database.sqlite3') | |
create_merchant_table | |
@merchants_table = @database.from(:merchants) | |
load_merchant_table | |
end | |
def create_merchant_table | |
database.create_table? :merchants do | |
primary_key :id | |
String :name, :size => 100 | |
DateTime :created_at | |
DateTime :updated_at | |
end | |
end | |
def load_merchant_table | |
all.each do |row| | |
merchants_table.insert( | |
:name => row[:name], | |
:created_at => row[:created_at], | |
:updated_at => row[:updated_at] | |
) | |
end | |
end | |
def find_by_name(name) | |
merchants_table.where(:name => name) | |
end | |
def find_by(id) | |
merchants_table.where(:id => id) | |
end | |
def data | |
CSV.read("./data/merchants.csv", headers: true, header_converters: :symbol) | |
end | |
def all | |
@all ||= data.collect { |row| row } | |
end | |
def merchant_items | |
@database[:merchants].select(Sequel.as(:merchants__name, :merchant_name), Sequel.as(:items__name, :item_name)).join(:items, :merchant_id => :id) | |
end | |
def merchant_items_by_merchant(id) | |
merchant_items.where(:merchant_name) | |
end | |
def list_items_by(id) | |
# merchant_items.select(:where(:id => id) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment