diff --git a/lib/boot.rb b/lib/boot.rb new file mode 100644 index 0000000..44f726c --- /dev/null +++ b/lib/boot.rb @@ -0,0 +1,10 @@ +$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) +$LOAD_PATH.unshift(File.dirname(__FILE__)) + +require 'sqlite_adapter' +require 'empanada_record' +require "product" + +p Product.first +p Product.last +p Product.where("id = 20") \ No newline at end of file diff --git a/lib/empanada_record.rb b/lib/empanada_record.rb index b49fefe..1534291 100644 --- a/lib/empanada_record.rb +++ b/lib/empanada_record.rb @@ -3,7 +3,8 @@ module EmpanadaRecord class Base @@adapter = SqliteAdapter.new - + + # Finder methdos def self.find(id) results = @@adapter.run("SELECT * FROM #{table_name} WHERE id=#{id.to_i}") if results.any? @@ -13,8 +14,64 @@ def self.find(id) end end + def self.all + resultset = @@adapter.run("SELECT * FROM #{table_name}") + resultset.map { |ary| self.new(*ary)} + end + + def self.count + resultset = @@adapter.run("SELECT COUNT(*) FROM #{table_name}") + resultset.flatten.first + end + + def self.first + resultset = @@adapter.run("SELECT * FROM #{table_name} LIMIT 1 OFFSET 0") + if resultset.any? + self.new(*resultset.first) + else + nil + end + end + + def self.last + resultset = @@adapter.run("SELECT * FROM #{table_name} ORDER BY rowid DESC LIMIT 1 OFFSET 0") + if resultset.any? + self.new(*resultset.first) + else + nil + end + end + + def self.where(query) + resultset = @@adapter.run("SELECT * FROM #{table_name} WHERE #{query}") + resultset.map { |ary| self.new(*ary)} + end + def self.table_name self.name + "s" end + + def self.find_by(attribute, data ) + #Object.const_get("Product") + if respond_to?(attribute.to_s) + results = @@adapter.run("SELECT * FROM #{table_name} WHERE #{attribute} = '#{data}'") + if results.any? + results.map { |ary| self.new(*ary)} + else + raise 'EmpanadaRecordError: Record Not Found!' + end + else + raise "EmpanadaRecordError: undefined method '#{attribute}'" + end + end + + def self.attributes + attrs = @@adapter.run("pragma table_info(#{table_name})") + attrs.map {|attr| attr[1]} + end + + def self.respond_to?(attribute) + attributes.include? attribute + end end end diff --git a/test/test_product.rb b/test/test_product.rb index 2520e34..660df1f 100644 --- a/test/test_product.rb +++ b/test/test_product.rb @@ -18,4 +18,40 @@ def test_find_no_records end assert_equal err.message, 'EmpanadaRecordError: Record Not Found!' end + + def test_all + ids = Product.all.map { |p| p.id } + assert_equal ids.sort, [10, 20] + end + + def test_count + count = Product.count + assert_equal count, 2 + end + + def test_first + assert_equal Product.first.id, 20 + end + + def test_last + assert_equal Product.last.id, 10 + end + + def test_where + product = Product.where("id = 20") + assert_equal product.first.id, 20 + end + + def test_find_by + products = Product.find_by("name", "Portatil") + assert_operator products.count, :>=, 1 + end + + def test_attributes + assert_equal Product.attributes.sort, ["id", "name"] + end + + def test_respond_to? + assert_equal Product.respond_to?("name"), true + end end