Skip to content

Commit

Permalink
move code to /lib
Browse files Browse the repository at this point in the history
  • Loading branch information
brianstorti committed Mar 9, 2015
1 parent 710b8d4 commit 7eb70ac
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion config.ru
@@ -1,3 +1,3 @@
require_relative 'todo'
require_relative 'lib/todo'

run TodoAPI
File renamed without changes.
44 changes: 44 additions & 0 deletions lib/todo_repo.rb
@@ -0,0 +1,44 @@
RecordNotFound = Class.new(StandardError)

class TodoRepo
class << self
def todos
@todos ||= []
@todos
end

def add(params, host)
todo = Hashie.symbolize_keys!(params.to_hash)
id = SecureRandom.uuid
todo[:id] = id
todo[:url] = "#{host}/#{id}"

todos << todo
todo
end

def find(id)
todos.detect {|todo| todo[:id] == id}
end

def update(params)
params = Hashie.symbolize_keys(params.to_hash)
todo = find(params[:id])
raise RecordNotFound.new unless todo

todo.merge!(params)
todo
end

def delete(id)
todo = find(id)
raise RecordNotFound.new unless todo

todos.delete(todo)
end

def delete_all
@todos = []
end
end
end
2 changes: 1 addition & 1 deletion test/test_todo_api.rb
Expand Up @@ -2,7 +2,7 @@

require 'rack/test'
require 'minitest/autorun'
require_relative '../todo.rb'
require_relative '../lib/todo.rb'

class TodoAPITest < MiniTest::Test
include Rack::Test::Methods
Expand Down

0 comments on commit 7eb70ac

Please sign in to comment.