Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comments are welcome! #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions lib/empanada_record.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,53 @@
require 'sqlite_adapter'
require 'missing_empanada_record'
require 'empanada_record_attributes'

module EmpanadaRecord
class Base
ATTRIBUTES = ['id', 'name']
@@adapter = SqliteAdapter.new


class << self
ATTRIBUTES.each do |attr|
define_method "find_by_#{attr}" do |argument|
results = @@adapter.run("SELECT * FROM #{table_name} WHERE #{attr} = '#{argument}'")
results.any? ? self.new(*results.first) : MissingEmpanadaRecord.new
end
end
end

def method_missing(name, *args) "Not method available for empanada record" end

def self.find(id)
results = @@adapter.run("SELECT * FROM #{table_name} WHERE id=#{id.to_i}")
if results.any?
self.new(*results.first)
else
raise 'EmpanadaRecordError: Record Not Found!'
end
results.any? ? self.new(*results.first) : MissingEmpanadaRecord.new
end

def self.first
result = @@adapter.run("SELECT * FROM #{table_name} LIMIT 1")
result.any? ? self.new(*result.first) : MissingEmpanadaRecord.new
end

def self.all
results = @@adapter.run("SELECT * FROM #{table_name}")
results.any? ? results.map! {|product| self.new(*product)} : [MissingEmpanadaRecord.new]
end

def self.count
result = @@adapter.run("SELECT COUNT(*) FROM #{table_name}")
result.flatten.first
end

def self.where(args)
attributes = EmpanadaRecordAttributes.new(args.keys)
valid_attributes = attributes.validate
raise ArgumentError.new(valid_attributes) if valid_attributes.is_a? String

conditions = []
args.each{ |key, value| conditions << "#{key} = '#{value}'"}

results = @@adapter.run("SELECT * FROM #{table_name} WHERE #{conditions.join(" AND ")}")
results.any? ? results.map! {|product| self.new(*product)} : [MissingEmpanadaRecord.new]
end

def self.table_name
Expand Down
17 changes: 17 additions & 0 deletions lib/empanada_record_attributes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class EmpanadaRecordAttributes
attr_accessor :attributes
ATTRIBUTES = ['id', 'name']

def initialize(attributes)
@attributes = attributes.collect(&:to_s)
end

def validate
invalid_attributes = @attributes.select{ |attr| !ATTRIBUTES.include?(attr) }
invalid_attributes.any? ? invalid_attributes_message(invalid_attributes) : true
end

def invalid_attributes_message(attributes)
"Next attributes doesn't exist: #{attributes.join(',')}"
end
end
14 changes: 14 additions & 0 deletions lib/missing_empanada_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class MissingEmpanadaRecord
ATTRIBUTES = [:id, :name]

ATTRIBUTES.each do |attr|
define_method "#{attr}" do get_message end
end

def method_missing(name, *args) get_message end

private
def get_message
"EmpanadaRecordError: Record Not Found!"
end
end
84 changes: 80 additions & 4 deletions test/test_product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,85 @@ def test_find_by_id
end

def test_find_no_records
err = assert_raises RuntimeError do
Product.find(999)
end
assert_equal err.message, 'EmpanadaRecordError: Record Not Found!'
product = Product.find(9929)
assert_instance_of MissingEmpanadaRecord, product
assert_equal product.name, 'EmpanadaRecordError: Record Not Found!'
end

def test_find_first_record
product = Product.first

refute_nil product
assert_equal product.id, 20
end

def test_find_all_records
products = Product.all

refute_nil products
assert_equal products.class, Array
assert_equal products.size, 2
end

def test_count_records
number_of_products = Product.count

assert_equal Fixnum, number_of_products.class
assert_equal number_of_products, 2
end

def test_find_by_id
product = Product.find_by_id("20")

assert_equal product.id, 20
assert_equal product.name, 'Portatil'
assert_instance_of Product, product
end

def test_find_by_name
product = Product.find_by_name("Portatil")

assert_equal product.id, 20
assert_equal product.name, 'Portatil'
assert_instance_of Product, product
end

def test_find_by_id_no_record_found
product = Product.find_by_id("20123")

assert_instance_of MissingEmpanadaRecord, product
assert_equal product.id, 'EmpanadaRecordError: Record Not Found!'
assert_equal product.name, 'EmpanadaRecordError: Record Not Found!'
end

def test_find_by_name_no_record_found
product = Product.find_by_name("Empanada")

assert_instance_of MissingEmpanadaRecord, product
assert_equal product.id, 'EmpanadaRecordError: Record Not Found!'
assert_equal product.name, 'EmpanadaRecordError: Record Not Found!'
end

def test_where_method
products = Product.where(:id => 20, :name => "Portatil")

assert_equal products.size, 1
assert_equal products.first.id, 20
assert_equal products.first.name, 'Portatil'
assert_instance_of Product, products.first
end

def test_where_method_invalid_attributes
assert_raises(ArgumentError) {
products = Product.where(:random_attribute => 20, :nmas => "Portatil")
}
end

def test_where_method_no_records_found
products = Product.where(:id => 220, :name => "Empanada")

assert_equal products.first.id, 'EmpanadaRecordError: Record Not Found!'
assert_equal products.first.name, 'EmpanadaRecordError: Record Not Found!'
assert_instance_of MissingEmpanadaRecord, products.first
end
end