-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a solution for the sinatra resource
- Loading branch information
Showing
8 changed files
with
180 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
require "sinatra" | ||
|
||
class Member | ||
attr_reader :name | ||
|
||
def initialize(name = nil) | ||
@name = name | ||
end | ||
end | ||
|
||
class MemberValidator | ||
attr_reader :name, :names, :messages | ||
|
||
def initialize(name, names) | ||
@name = name.to_s | ||
@names = names | ||
@messages = [] | ||
end | ||
|
||
def valid? | ||
validate | ||
messages.empty? | ||
end | ||
|
||
private | ||
|
||
def validate | ||
if name.empty? | ||
messages << "You need to enter a name" | ||
elsif names.include?(name) | ||
messages << "#{name} is already included in our list." | ||
end | ||
end | ||
end | ||
|
||
FILENAME = "names.txt" | ||
|
||
def members | ||
names.map { |name| Member.new(name) } | ||
end | ||
|
||
def names | ||
return [] unless File.exists?(FILENAME) | ||
File.read(FILENAME).split("\n") | ||
end | ||
|
||
def find_member(name) | ||
members.detect { |member| member.name == name } | ||
end | ||
|
||
def add_member(name) | ||
File.open(FILENAME, "a+") do |file| | ||
file.puts(name) | ||
end | ||
end | ||
|
||
def remove_member(name) | ||
lines = names.reject { |other| name == other } | ||
|
||
File.open(FILENAME, "w+") do |file| | ||
file.puts(lines.join("\n")) | ||
end | ||
end | ||
|
||
get "/members" do | ||
@members = members | ||
erb :index | ||
end | ||
|
||
get "/members/new" do | ||
@member = Member.new | ||
erb :new | ||
end | ||
|
||
post "/members" do | ||
name = params[:name] | ||
validator = MemberValidator.new(name, names) | ||
|
||
if validator.valid? | ||
add_member(name) | ||
redirect "/members/#{name}" | ||
else | ||
@member = Member.new(name) | ||
@messages = validator.messages | ||
erb :new | ||
end | ||
end | ||
|
||
get "/members/:name" do | ||
@member = find_member(params[:name]) | ||
erb :show | ||
end | ||
|
||
get "/members/:name/edit" do | ||
@member = find_member(params[:name]) | ||
erb :edit | ||
end | ||
|
||
put "/members/:name" do | ||
@member = find_member(params[:name]) | ||
end | ||
|
||
get "/members/:name/delete" do | ||
@member = find_member(params[:name]) | ||
erb :delete | ||
end | ||
|
||
delete "/members/:name" do | ||
remove_member(params[:name]) | ||
redirect "/members" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Anja | ||
Carla | ||
Maren | ||
Rebecca | ||
Sabrina |
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,13 @@ | ||
<p> | ||
<a href="/members">All Members</a> | | ||
<a href="/members/<%= @member.name %>"><%= @member.name %></a> | ||
</p> | ||
|
||
<h1>Removing Member: <%= @member.name %></h1> | ||
|
||
<p>Are you sure you want to remove the member <%= @member.name %>?</p> | ||
|
||
<form action="/members/<%= @member.name %>" method="post"> | ||
<input type="hidden" name="_method" value="delete"> | ||
<input type="submit" value="Yes, remove"> | ||
</form> |
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,12 @@ | ||
<p> | ||
<a href="/members">All Members</a> | | ||
<a href="/members/<%= @member.name %>"><%= @member.name %></a> | ||
</p> | ||
|
||
<h1>Update Member: <%= @member.name %></h1> | ||
|
||
<form action="/members/<%= @member.name %>" method="post"> | ||
<input type="hidden" name="_method" value="put"> | ||
<input type="text" name="name" value="<%= @member.name %>"> | ||
<input type="submit"> | ||
</form> |
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,13 @@ | ||
<h1>Members</h1> | ||
|
||
<p><a href="/members/new">New Member</a></p> | ||
|
||
<ul> | ||
<% @members.each do |member| %> | ||
<li> | ||
<a href="/members/<%= member.name %>"><%= member.name %></a> | ||
<a href="/members/<%= member.name %>/edit">Edit</a> | ||
<a href="/members/<%= member.name %>/delete">Remove</a> | ||
</li> | ||
<% end %> | ||
</ul> |
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,5 @@ | ||
<html> | ||
<body> | ||
<%= yield %> | ||
</body> | ||
</html> |
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,16 @@ | ||
<p><a href="/members">All Members</a></p> | ||
|
||
<h1>New Member</h1> | ||
|
||
<% if @messages %> | ||
<ul> | ||
<% @messages.each do |message| %> | ||
<li><%= message %></li> | ||
<% end %> | ||
</ul> | ||
<% end %> | ||
|
||
<form action="/members" method="post"> | ||
<input type="text" name="name" value="<%= @member.name %>"> | ||
<input type="submit"> | ||
</form> |
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,5 @@ | ||
<p><a href="/members">All Members</a></p> | ||
|
||
<h1>Member: <%= @member.name %></h1> | ||
|
||
<p>Name: <%= @member.name %></p> |