From 1b3e2cc6943358e919ae894adbfcaec3e2aa78d1 Mon Sep 17 00:00:00 2001 From: Pedro Alvarado Rincon Date: Tue, 5 May 2015 16:51:42 -0500 Subject: [PATCH 1/4] boot file --- lib/boot.rb | 8 ++++++++ lib/empanada_record.rb | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 lib/boot.rb diff --git a/lib/boot.rb b/lib/boot.rb new file mode 100644 index 0000000..7b858ce --- /dev/null +++ b/lib/boot.rb @@ -0,0 +1,8 @@ +$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.all \ No newline at end of file diff --git a/lib/empanada_record.rb b/lib/empanada_record.rb index b49fefe..d970387 100644 --- a/lib/empanada_record.rb +++ b/lib/empanada_record.rb @@ -3,7 +3,7 @@ module EmpanadaRecord class Base @@adapter = SqliteAdapter.new - + def self.find(id) results = @@adapter.run("SELECT * FROM #{table_name} WHERE id=#{id.to_i}") if results.any? From c1c17583cac21de38f662a1232ae19a5ac7f4dee Mon Sep 17 00:00:00 2001 From: Pedro Alvarado Rincon Date: Wed, 6 May 2015 08:43:35 -0500 Subject: [PATCH 2/4] Including #all method --- lib/empanada_record.rb | 5 +++++ test/test_product.rb | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/lib/empanada_record.rb b/lib/empanada_record.rb index d970387..fe46334 100644 --- a/lib/empanada_record.rb +++ b/lib/empanada_record.rb @@ -13,6 +13,11 @@ 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.table_name self.name + "s" end diff --git a/test/test_product.rb b/test/test_product.rb index 2520e34..8197d13 100644 --- a/test/test_product.rb +++ b/test/test_product.rb @@ -18,4 +18,9 @@ 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 end From 42c3e1580131d3f0ca90a64523a7670b82183da1 Mon Sep 17 00:00:00 2001 From: Pedro Alvarado Rincon Date: Wed, 6 May 2015 09:57:31 -0500 Subject: [PATCH 3/4] Implementing count, first, last and where methods --- lib/boot.rb | 4 +++- lib/empanada_record.rb | 28 ++++++++++++++++++++++++++++ test/test_product.rb | 19 +++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/lib/boot.rb b/lib/boot.rb index 7b858ce..44f726c 100644 --- a/lib/boot.rb +++ b/lib/boot.rb @@ -5,4 +5,6 @@ require 'empanada_record' require "product" -p Product.all \ No newline at end of file +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 fe46334..acf787a 100644 --- a/lib/empanada_record.rb +++ b/lib/empanada_record.rb @@ -18,6 +18,34 @@ def self.all 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 diff --git a/test/test_product.rb b/test/test_product.rb index 8197d13..0f1409f 100644 --- a/test/test_product.rb +++ b/test/test_product.rb @@ -23,4 +23,23 @@ 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 + end From bb7ffb675218894e539006cd9b1c4e62fb437c8c Mon Sep 17 00:00:00 2001 From: Pedro Alvarado Rincon Date: Wed, 6 May 2015 17:50:50 -0500 Subject: [PATCH 4/4] Adding find_by, attributes, respond_to? methods --- lib/empanada_record.rb | 24 ++++++++++++++++++++++++ test/test_product.rb | 12 ++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/empanada_record.rb b/lib/empanada_record.rb index acf787a..1534291 100644 --- a/lib/empanada_record.rb +++ b/lib/empanada_record.rb @@ -4,6 +4,7 @@ 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? @@ -49,5 +50,28 @@ def self.where(query) 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 0f1409f..660df1f 100644 --- a/test/test_product.rb +++ b/test/test_product.rb @@ -42,4 +42,16 @@ def test_where 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