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

Add circle.yml file #7

Open
wants to merge 1 commit 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
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source "https://rubygems.org"

gem 'bloc_record', path: '../bloc_record'

gem 'pg'
25 changes: 25 additions & 0 deletions address_bloc.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
require_relative 'controllers/menu_controller'
require 'bloc_record'

BlocRecord.connect_to("address_bloc", :pg)

menu = MenuController.new
system "clear"
puts "Welcome to AddressBloc!"
menu.main_menu

class DomesticCat
def jump(thisHigh)
puts "I jumped #{thisHigh}"
end
end

class WildCat
def leap(howHigh)
puts "I leaped #{thisHigh}"
end
end

catType = :wild

if catType == :domestic
cat = DomesticCat.new
elsif catType == :wild
cat = WildCat.new
end

cat.jump("really high")
6 changes: 6 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dependencies:
pre:
- gem install rspec
test:
post:
- rspec
121 changes: 110 additions & 11 deletions controllers/menu_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@ 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"
puts "4 - Import entries from a CSV"
puts "5 - Exit"
puts "6 - Test"
print "Enter your selection: "

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 @@ -38,16 +44,101 @@ def main_menu
when 5
puts "Good-bye!"
exit(0)
when 6
test_stuff
exit(0)
else
system "clear"
puts "Sorry, that is not a valid input"
main_menu
end
end

def test_stuff
originals = {}
news = {
1 => {
"name" => "Foo Uno",
"email" => "[email protected]"
},
2 => {
"name" => "Foo Dos",
"email" => "[email protected]"
}
}

puts "---- Original 1 and 2 ----"

Entry.find(news.keys).each do |entry|
puts entry
originals[entry.id] = {
"name" => entry.name,
"email" => entry.email
}
end

# Entry.update([1, 2], [{name => Foo Uno}, {name => Foo Dos}])
#
# Entry.update(1, {name => Foo Uno})
# Entry.update(2, {name => Foo Dos})
Entry.update(news.keys, news.values)

puts "---- New 1 and 2 ----"
puts Entry.where

Entry.update(originals.keys, originals.values)

puts "---- Back to Original 1 and 2 ----"
Entry.find_each do |item|
puts item
end

Entry.where(id: 3).update_all(
name: "Foo Tres",
email: "[email protected]"
)

puts "---- 3's New Name ----"
puts Entry.where(name: "Foo Tres") # Should return a collection with just Mr. Unique

puts "---- Everyone Besides New 3 ----"
puts Entry.not(name: "Foo Tres") # Should return a collection with everyone but Mr. Unique)

Entry.find(3).update_name("Foo Three")
Entry.find_one(3).update_email("[email protected]")

puts "---- Back to Original 3 ----"
puts Entry.find_by_name("Foo Three")

puts "---- Again, Everyone Else (Should be unchanged) ----"
puts Entry.where(address_book_id: 1).not(name: "Foo Three")

puts "---- All Together Now ----"
puts Entry.find_all

puts "---- Delete Mitch Moreland ----"
puts Entry.destroy_all("name = 'Mitch Moreland'")
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 "Address Book: #{entry.address_book.name} Entry"
puts entry.to_s
entry_submenu(entry)
end
Expand Down Expand Up @@ -75,7 +166,7 @@ def create_entry
def search_entries
print "Search by name: "
name = gets.chomp
match = address_book.binary_search(name)
match = Entry.find_by(:name, name)
system "clear"
if match
puts match.to_s
Expand Down Expand Up @@ -130,24 +221,33 @@ def entry_submenu(entry)
end
end

def delete_entry(entry)
address_book.entries.delete(entry)
puts "#{entry.name} has been deleted"
# Orinal `delete_entry` :
# def delete_entry(entry)
# address_book.entries.delete(entry)
# puts "#{entry.name} has been deleted"
# end

def delete_entry(entry_id)
self.destroy(entry_id)
end

def edit_entry(entry)
updates = {}
print "Updated name: "
name = gets.chomp
updates[:name] = name unless name.empty?
print "Updated phone number: "
phone_number = gets.chomp
updates[:phone_number] = phone_number unless phone_number.empty?
print "Updated email: "
email = gets.chomp
entry.name = name if !name.empty?
entry.phone_number = phone_number if !phone_number.empty?
entry.email = email if !email.empty?
updates[:email] = email unless email.empty?

entry.update_attributes(updates)
system "clear"
puts "Updated entry:"
puts entry
puts Entry.find(entry.id)
end

def search_submenu(entry)
Expand Down Expand Up @@ -176,4 +276,3 @@ def search_submenu(entry)
end
end
end

13 changes: 13 additions & 0 deletions db/seed_pg.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('address_bloc', :pg)

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]' )
39 changes: 7 additions & 32 deletions models/address_book.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
require_relative 'entry'
require "csv"
require 'bloc_record/base'

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

def initialize
@entries = []
def add_entry(name, phone_number, email)
Entry.create(name: name, phone_number: phone_number, 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 find_entry(name)
Entry.where(name: name, address_book_id: self.id).first
end

def import_from_csv(file_name)
Expand All @@ -29,24 +23,5 @@ 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,12 +1,7 @@
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
belongs_to :address_book

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