Skip to content

Instantly share code, notes, and snippets.

@sdevani
Created May 12, 2014 19:53
Show Gist options
  • Save sdevani/f1333a08a924ef5962b4 to your computer and use it in GitHub Desktop.
Save sdevani/f1333a08a924ef5962b4 to your computer and use it in GitHub Desktop.
Creating Projects using a database class (no database included)
module TM
class DB
attr_reader :projects
def initialize
@projects = {}
@project_count = 0
end
def create_project(data)
@project_count += 1
data[:id] = @project_count
# I've commented out how I would create the complete and time_create
# attributes for tasks
#
# data[:complete] = false
# data[:time_created] = Time.now
@projects[ data[:id] ] = data
TM::Project.new(data[:name], data[:id])
# The line below would be used if we had a complete attribute
# TM::Project.new(data[:name], data[:id], data[:complete])
end
def get_project(id)
data = @projects[id]
if !data.nil?
TM::Project.new(data[:name], data[:id])
end
end
def update_project(id, data)
old_data = @projects[id]
old_data.merge!(data)
end
def destroy_project(id)
@projects.delete(id)
end
# >>>>>>>>>NOTE<<<<<<<<< This method does not work.
# I created this as a demo of how this method would work
def get_completed_tasks(project_id)
results = []
@tasks.each do |task|
if task[:project_id] == project_id && task[:complete]
results << get_task(task[:id])
end
end
results.sort_by()
end
end
def self.db
@__db_instance ||= TM::DB.new
end
end
require 'spec_helper.rb'
describe TM::DB do
describe 'db as a singleton' do
it "returns a DB object" do
db = TM.db
expect(db).to be_a(TM::DB)
end
it "returns same db object every time" do
db = TM.db
db2 = TM.db
expect(db).to be(db2)
end
end
describe "projects inside db" do
it "contains storage for projects" do
expect(TM.db.projects).to be_a(Hash)
end
describe "#create_project" do
let(:project) {TM.db.create_project(:name => "My Project")}
it "creates a new project entity" do
expect(project).to be_a(TM::Project)
expect(project.id).to be_a(Fixnum)
expect(project.name).to eq("My Project")
end
it "stores information in the db" do
expect(TM.db.projects[project.id]).to eq({
:name => "My Project",
:id => project.id
})
end
it "gives a unique id every time" do
p1 = TM.db.create_project(:name => "p1")
p2 = TM.db.create_project(:name => "p1")
expect(p1.id).to_not eq(p2.id)
end
end
describe "#get_project" do
it "returns a project entity with the proper data" do
p1 = TM.db.create_project(:name => "p1")
p2 = TM.db.create_project(:name => "p1")
project = TM.db.get_project(p1.id)
expect(project).to be_a(TM::Project)
expect(p1.id).to eq(project.id)
expect(p1.name).to eq(project.name)
end
it "returns nil if the project doesn't exist" do
project = TM.db.get_project(3)
expect(project).to eq(nil)
end
end
describe "#update_project" do
it "updates the project in the db" do
p1 = TM.db.create_project(:name => "p1")
# t1 = TM.db.create_task(:project_id => 1, :description: "blah", :priority_num => 3)
TM.db.update_project(p1.id, :name => "Hello")
project = TM.db.get_project(p1.id)
expect(project.name).to eq("Hello")
expect(project.id).to eq(p1.id)
end
end
describe "#destroy_project" do
it "destroys project from db" do
p1 = TM.db.create_project(:name => "p1")
TM.db.destroy_project(p1.id)
project = TM.db.get_project(p1.id)
expect(project).to eq(nil)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment