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

First aproach #1

Open
wants to merge 4 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
10 changes: 10 additions & 0 deletions lib/boot.rb
Original file line number Diff line number Diff line change
@@ -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")
59 changes: 58 additions & 1 deletion lib/empanada_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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
36 changes: 36 additions & 0 deletions test/test_product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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