Skip to content

Commit

Permalink
add a solution for the sinatra resource
Browse files Browse the repository at this point in the history
  • Loading branch information
svenfuchs committed May 24, 2015
1 parent 4a7179c commit ef976a7
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 0 deletions.
111 changes: 111 additions & 0 deletions solutions/sinatra_resource/members.rb
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
5 changes: 5 additions & 0 deletions solutions/sinatra_resource/names.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Anja
Carla
Maren
Rebecca
Sabrina
13 changes: 13 additions & 0 deletions solutions/sinatra_resource/views/delete.erb
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>
12 changes: 12 additions & 0 deletions solutions/sinatra_resource/views/edit.erb
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>
13 changes: 13 additions & 0 deletions solutions/sinatra_resource/views/index.erb
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>
5 changes: 5 additions & 0 deletions solutions/sinatra_resource/views/layout.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<%= yield %>
</body>
</html>
16 changes: 16 additions & 0 deletions solutions/sinatra_resource/views/new.erb
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>
5 changes: 5 additions & 0 deletions solutions/sinatra_resource/views/show.erb
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>

0 comments on commit ef976a7

Please sign in to comment.