diff --git a/lib/empanada_record.rb b/lib/empanada_record.rb index b49fefe..d63d18c 100644 --- a/lib/empanada_record.rb +++ b/lib/empanada_record.rb @@ -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 diff --git a/lib/empanada_record_attributes.rb b/lib/empanada_record_attributes.rb new file mode 100644 index 0000000..53df39d --- /dev/null +++ b/lib/empanada_record_attributes.rb @@ -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 \ No newline at end of file diff --git a/lib/missing_empanada_record.rb b/lib/missing_empanada_record.rb new file mode 100644 index 0000000..669acaf --- /dev/null +++ b/lib/missing_empanada_record.rb @@ -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 \ No newline at end of file diff --git a/test/test_product.rb b/test/test_product.rb index 2520e34..a23297b 100644 --- a/test/test_product.rb +++ b/test/test_product.rb @@ -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