Created
April 15, 2020 20:20
-
-
Save RickArora/d624da29d46169d5433ed0f15a086fc4 to your computer and use it in GitHub Desktop.
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
Failures: | |
1) Play find by titile returns the Play matching the given title | |
Failure/Error: expect(Play.find_by_title('All My Sons').year).to eq(1947) | |
NoMethodError: | |
undefined method `find_by_title' for Play:Class | |
# ./spec/plays_spec.rb:7:in `block (3 levels) in <top (required)>' | |
# failing test below: | |
require 'plays' | |
describe Play do | |
describe 'find by titile' do | |
subject (:list_of_plays) {Play.new} | |
it 'returns the Play matching the given title' do | |
expect(Play.find_by_title('All My Sons').year).to eq(1947) | |
end | |
end | |
end | |
# plays.rb below: | |
require 'sqlite3' | |
require 'singleton' | |
class PlayDBConnection < SQLite3::Database | |
include Singleton | |
def initialize | |
super('../plays.db') | |
self.type_translation = true | |
self.results_as_hash = true | |
end | |
end | |
class Play | |
attr_accessor :id, :title, :year, :playwright_id | |
def self.all | |
data = PlayDBConnection.instance.execute("SELECT * FROM plays") | |
data.map { |datum| Play.new(datum) } | |
end | |
def initialize(options) | |
@id = options['id'] | |
@title = options['title'] | |
@year = options['year'] | |
@playwright_id = options['playwright_id'] | |
end | |
def create | |
raise "#{self} already in database" if self.id | |
PlayDBConnection.instance.execute(<<-SQL, self.title, self.year, self.playwright_id) | |
INSERT INTO | |
plays (title, year, playwright_id) | |
VALUES | |
(?, ?, ?) | |
SQL | |
self.id = PlayDBConnection.instance.last_insert_row_id | |
end | |
def update | |
raise "#{self} not in database" unless self.id | |
PlayDBConnection.instance.execute(<<-SQL, self.title, self.year, self.playwright_id, self.id) | |
UPDATE | |
plays | |
SET | |
title = ?, year = ?, playwright_id = ? | |
WHERE | |
id = ? | |
SQL | |
end | |
def find_by_title(name) | |
name_search = PlayDBConnection.instance.execute(<<-SQL, name) | |
SELECT | |
* | |
FROM | |
plays | |
WHERE | |
name = ? | |
SQL | |
name_search.map{ |play| Play.new(play) } | |
end | |
def find_by_playwright(name) | |
end | |
end | |
# full code repo https://github.com/RickArora/Postgres/tree/master/Plays-Playwrights%20ORM | |
#help plz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment