I recently wrote my first webapp and here is the code. I want to make it better, but I'm not exactly sure how. I believe the first place I should start is with the structure of it. Is this spaghetti code? If yes, how do I change that? You can view it at http://shiftfrog.com
I created a class called 'doer' that basically drives the program. Is that wrong? Should my class be called calendar and then manipulate in my post call to /calendar?
class Doer
def makeDate(date)
return Date.strptime(date, "%m/%d/%Y")
end
def buildArray(dateObj, on, off)
array = frontpadding(dateObj, on, off)
month = dateObj.month
newMonth = month
day = dateObj.mday
date = dateObj
while month == newMonth
temp_array = [day,'day']
array.push(temp_array)
day = day + 1
date = date + 1
newMonth = date.month
end
endpadding(date, array)
array
end
def endpadding(dateObj, array)
dofw = dateObj.wday
filler = (dofw - 6).abs + 1
if filler == 7
#do nothing
else
until filler == 0
temp_array = ['','day']
array.push(temp_array)
filler = filler - 1
end
end
end
def frontpadding(dateObj, on, off)
array = Array.new
day = dateObj.mday
firstOfMonth = Date.new(dateObj.year, dateObj.month)
filler = firstOfMonth.wday
on = on.to_i
off = off.to_i
mod = on + off
if day != 1
@days = day
@location = @days + filler
until day <= 1
off.times do
if day > 1
temp_array = ['','day']
array.unshift(temp_array)
day = day - 1
end
end
on.times do
if day > 1
temp_array = ['','dayOn']
array.unshift(temp_array)
day = day - 1
end
end
end
end
until filler == 0
temp_array = ['','day']
array.unshift(temp_array)
filler = filler - 1
end
array
end
def backFill(cal, on, off)
@location = @location.to_i
if @location > 0
@location = @location - 1
until @days == 0
cal[0][1][@location][0] = @days
@days = @days - 1
@location = @location - 1
end
end
cal
end
def makeCal(date, on, off)
dateObj = makeDate(date)
@cal = Array.new
months = 0
while months < 13
# #pass dateobj to build array
array = buildArray(dateObj, on, off)
# #save array to hash with month key
monthName = Date::MONTHNAMES[dateObj.mon]
monthYear = "#{monthName} " + dateObj.year.to_s
holder = Array.new
holder.push monthYear
holder.push array
@cal.push holder
# #create new date object using month and set it to the first
dateObj = Date.new(dateObj.year, dateObj.month)
dateObj >>= 1
months = months + 1
end
@cal = createCal(@cal, on, off)
@cal = backFill(@cal, on, off)
end
def createCal(cal, on, off)
on = on.to_i
off = off.to_i
@mod = on + off
daycount = 0
cal.each do |month|
month[1].each do |day|
if day[0] == ''
#do nothing
else
if daycount % @mod < on
day[1] = 'dayOn'
end
daycount = daycount + 1
end
end
end
cal
end
end
get '/' do
haml :ask
end
post '/calendar' do
@on = params["on"]
@off = params["off"]
@date = params["date"]
a = Doer.new
@cal = a.makeCal(@date, @on, @off)
haml :feeling
end