Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
710b8d4
commit 7eb70ac
Showing
4 changed files
with
46 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
require_relative 'todo' | ||
require_relative 'lib/todo' | ||
|
||
run TodoAPI |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters