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

03 where order joins #13

Open
wants to merge 5 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
8 changes: 8 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

# gem "rails"
gem 'bloc_record', path: '../bloc_record'
19 changes: 19 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
PATH
remote: ../bloc_record
specs:
bloc_record (0.0.0)
sqlite3 (~> 1.3)

GEM
remote: https://rubygems.org/
specs:
sqlite3 (1.3.13)

PLATFORMS
ruby

DEPENDENCIES
bloc_record!

BUNDLED WITH
1.16.0.pre.3
3 changes: 3 additions & 0 deletions address_bloc.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
require_relative 'controllers/menu_controller'
require 'bloc_record'

BlocRecord.connect_to("db/address_bloc.sqlite")

menu = MenuController.new
system "clear"
Expand Down
30 changes: 25 additions & 5 deletions controllers/menu_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ class MenuController
attr_reader :address_book

def initialize
@address_book = AddressBook.new
@address_book = AddressBook.first
end

def main_menu
puts "Main Menu - #{address_book.entries.count} entries"
puts "#{@address_book.name} Address Book Selected\n#{@address_book.entries.count} entries"
puts "0 - Switch AddressBook"
puts "1 - View all entries"
puts "2 - Create an entry"
puts "3 - Search for an entry"
Expand All @@ -19,6 +20,10 @@ def main_menu
selection = gets.to_i

case selection
when 0
system "clear"
select_address_book_menu
main_menu
when 1
system "clear"
view_all_entries
Expand All @@ -45,8 +50,24 @@ def main_menu
end
end


def select_address_book_menu
puts "Select an Address Book:"
AddressBook.all.each_with_index do |address_book, index|
puts "#{index} - #{address_book.name}"
end

index = gets.chomp.to_i

@address_book = AddressBook.find(index + 1)
system "clear"
return if @address_book
puts "Please select a valid index"
select_address_book_menu
end

def view_all_entries
address_book.entries.each do |entry|
@address_book.entries.each do |entry|
system "clear"
puts entry.to_s
entry_submenu(entry)
Expand Down Expand Up @@ -75,7 +96,7 @@ def create_entry
def search_entries
print "Search by name: "
name = gets.chomp
match = address_book.binary_search(name)
match = @address_book.find_entry(name)
system "clear"
if match
puts match.to_s
Expand Down Expand Up @@ -176,4 +197,3 @@ def search_submenu(entry)
end
end
end

Binary file added db/address_bloc.sqlite
Binary file not shown.
24 changes: 24 additions & 0 deletions db/create_tables.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'sqlite3'

db = SQLite3::Database.new "db/address_bloc.sqlite"

db.execute("DROP TABLE IF EXISTS address_book;");
db.execute("DROP TABLE IF EXISTS entry;");

db.execute <<-SQL
CREATE TABLE address_book (
id INTEGER PRIMARY KEY,
name VARCHAR(30)
);
SQL

db.execute <<-SQL
CREATE TABLE entry (
id INTEGER PRIMARY KEY,
address_book_id INTEGER,
name VARCHAR(30),
phone_number VARCHAR(30),
email VARCHAR(30),
FOREIGN KEY (address_book_id) REFERENCES address_book(id)
);
SQL
13 changes: 13 additions & 0 deletions db/seed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require_relative '../models/address_book'
require_relative '../models/entry'
require 'bloc_record'

BlocRecord.connect_to('db/address_bloc.sqlite')

book = AddressBook.create(name: 'My Address Book')

puts 'Address Book created'
puts 'Entry created'
puts Entry.create(address_book_id: book.id, name: 'Foo One', phone_number: '999-999-9999', email: '[email protected]' )
puts Entry.create(address_book_id: book.id, name: 'Foo Two', phone_number: '111-111-1111', email: '[email protected]' )
puts Entry.create(address_book_id: book.id, name: 'Foo Three', phone_number: '222-222-2222', email: '[email protected]' )
44 changes: 11 additions & 33 deletions models/address_book.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
require_relative 'entry'
require "csv"
require "bloc_record/base"

class AddressBook
attr_reader :entries
class AddressBook < BlocRecord::Base

def initialize
@entries = []
def add_entry(name, phone_number, email)
Entry.create(name: name, phone_number: phone, email: email, address_book_id: self.id)
end

def add_entry(name, phone_number, email)
index = 0
entries.each do |entry|
if name < entry.name
break
end
index += 1
end
entries.insert(index, Entry.new(name, phone_number, email))
def entries
Entry.order("name ASC, phone_number DESC")
# Entry.where(address_book_id: self.id)
end

def find_entry(name)
Entry.where(name: name, address_book_id: self.id).first
end

def import_from_csv(file_name)
Expand All @@ -29,24 +27,4 @@ def import_from_csv(file_name)
end
end

# Search AddressBook for a specific entry by name
def binary_search(name)
lower = 0
upper = entries.length - 1

while lower <= upper
mid = (lower + upper) / 2
mid_name = entries[mid].name

if name == mid_name
return entries[mid]
elsif name < mid_name
upper = mid - 1
elsif name > mid_name
lower = mid + 1
end
end

return nil
end
end
11 changes: 3 additions & 8 deletions models/entry.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
class Entry
# These must be accessors since we mutate them
attr_accessor :name, :phone_number, :email
require 'bloc_record/base'

def initialize(name, phone_number, email)
@name = name
@phone_number = phone_number
@email = email
end
class Entry < BlocRecord::Base

def to_s
"Name: #{name}\nPhone Number: #{phone_number}\nEmail: #{email}"
end

end