-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounter.rb
More file actions
68 lines (55 loc) · 1.24 KB
/
counter.rb
File metadata and controls
68 lines (55 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require 'rubygems'
require 'sinatra'
require 'dm-core'
require 'dm-migrations'
require 'Hasher.rb'
DataMapper.setup(:default, 'sqlite::memory:')
class Counter
include DataMapper::Resource
property :id, Serial
property :title, String
property :description, Text
property :created_at, DateTime
property :incrementing, Boolean
property :current_value, Integer
def tick
if self.incrementing
self.current_value = self.current_value + 1
else
self.current_value = self.current_value - 1
end
self.save unless self.id.nil?
self.current_value
end
def hash
unless self.id.nil?
Hasher::encode(self.id)
else
nil
end
end
end
get '/' do
DataMapper.auto_migrate!
haml :index
end
post '/create' do
values = Hash.new
if params[:starting_value].to_i
values[:current_value] = params[:starting_value]
end
values[:incrementing] = params[:direction] == 'Up'
if params[:title] != ""
values[:title] = params{:title}
end
if params[:description] != ""
values[:description] = params{:description}
end
counter = Counter.create(values)
unless counter.nil?
redirect "/#{counter.hash}"
else
@form_vals= params
haml :index
end
end