#!/usr/bin/env ruby
load "/home/ben/src/lib.rb/pphtml.rb"
require "pp"
require "yaml"
class Poll
attr_reader :head, :name, :hidden
def initialize name,hidden
@name = name
@hidden = hidden
@head = {}
@data = {}
@comment = []
store "Poll #{name} created"
end
def head_to_html
ret = "
\n"
@head.sort.each{|columntitle,columndescription|
ret += "
#{columntitle}
\n"
}
ret += "
Last Edit
\n"
ret += "
\n"
ret
end
def add_remove_column_htmlform
return <
END
end
def to_html
ret = "
\n"
ret += "\n"
ret += "
"
ret += "
"
unless @comment.empty?
ret += ""
end
ret += "
\n"
ret
end
def add_participant(name, agreed)
htmlname = CGI.escapeHTML(name.strip)
@data[htmlname] = {"timestamp" => Time.now}
@head.each_key{|columntitle|
@data[htmlname][columntitle] = agreed[columntitle.to_s]
}
store "Participant #{name} edited"
end
def invite_delete(name)
if @data.has_key?(name)
@data.delete(CGI.escapeHTML(name.strip))
store "Participant #{name} deleted"
else
add_participant(name,{})
end
end
def store comment
File.open("data.yaml", 'w') do |out|
out << "# This is a dudle poll file\n"
out << self.to_yaml
out.chmod(0660)
end
`export LC_ALL=de_DE.UTF-8; bzr commit -m '#{CGI.escapeHTML(comment)}'`
end
def add_comment name, comment
@comment << [Time.now, CGI.escapeHTML(name), CGI.escapeHTML(comment.strip).gsub("\r\n"," ")]
store "Comment added by #{name}"
end
def delete_comment index
store "Comment from #{@comment.delete_at(index)[1]} deleted"
end
def add_remove_column name, description
add_remove_parsed_column CGI.escapeHTML(name.strip), CGI.escapeHTML(description.strip)
end
def add_remove_parsed_column columntitle, description
if @head.include?(columntitle)
@head.delete(columntitle)
action = "deleted"
else
@head[columntitle] = description
action = "added"
end
store "Column #{columntitle} #{action}"
true
end
end
if __FILE__ == $0
require 'test/unit'
SITE = "glvhc_8nuv_8fchi09bb12a-23_uvc"
class Poll
attr_accessor :head, :data, :comment
end
class PollTest < Test::Unit::TestCase
def setup
@poll = Poll.new
end
def teardown
File.delete("#{SITE}.yaml") if File.exists?("#{SITE}.yaml")
end
def test_init
assert(@poll.head.empty?)
end
def test_add_participant
@poll.head["Item 2"] = ""
@poll.add_participant("bla",{"Item 2" => true})
assert_equal(Time, @poll.data["bla"]["timestamp"].class)
assert(@poll.data["bla"]["Item 2"])
end
def test_delete
@poll.data["bla"] = {}
@poll.delete(" bla ")
assert(@poll.data.empty?)
end
def test_store
@poll.add_remove_column("uaie","descriptionfoobar")
@poll.add_remove_column("gfia","")
@poll.add_participant("bla",{"uaie"=>"maybe", "gfia"=>"yes"})
@poll.add_comment("blabla","commentblubb")
@poll.store
assert_equal(@poll.data,YAML::load_file("#{SITE}.yaml").data)
assert_equal(@poll.head,YAML::load_file("#{SITE}.yaml").head)
assert_equal(@poll.comment,YAML::load_file("#{SITE}.yaml").comment)
end
def test_add_comment
@poll.add_comment("blabla","commentblubb")
assert_equal(Time, @poll.comment[0][0].class)
assert_equal("blabla", @poll.comment[0][1])
end
def test_add_remove_column
assert(@poll.add_remove_column(" bla ", ""))
assert(@poll.head.include?("bla"))
assert(@poll.add_remove_column(" bla ", ""))
assert(@poll.head.empty?)
end
end
end