diff options
author | Benjamin Kellermann <Benjamin.Kellermann@gmx.de> | 2008-11-07 21:44:05 +0100 |
---|---|---|
committer | Benjamin Kellermann <Benjamin.Kellermann@gmx.de> | 2008-11-07 21:44:05 +0100 |
commit | fc18c43ad3b324ae46c392b5a6da0e5f9c9637c6 (patch) | |
tree | e96b31ad62177d2cad9caf893e459908b80efde4 | |
parent | 1ac12b47ffbe986af51563d0a87218d0b62a7af1 (diff) |
added feature to sort multiple datafields
Diffstat (limited to '')
-rw-r--r-- | datepoll.rb | 26 | ||||
-rw-r--r-- | hash.rb | 37 | ||||
-rw-r--r-- | poll.rb | 53 |
3 files changed, 73 insertions, 43 deletions
diff --git a/datepoll.rb b/datepoll.rb index 939ca8a..344d122 100644 --- a/datepoll.rb +++ b/datepoll.rb @@ -2,17 +2,11 @@ require "date" require "poll" class DatePoll < Poll - def sort_data field - @data.sort{|x,y| - if field == "name" - x[0] <=> y[0] - elsif field == "timestamp" - x[1][field] <=> y[1][field] - else - datefield = Date.parse(field) - x[1][datefield].to_s <=> y[1][datefield].to_s - end + def sort_data fields + datefields = fields.collect{|field| + field == "timestamp" || field == "name" ? field : Date.parse(field) } + super datefields end def head_to_html ret = "<tr><td></td>\n" @@ -108,12 +102,16 @@ end if __FILE__ == $0 require 'test/unit' +class DatePoll + def store comment + end +end + +SITE="gbfuaibe" + class DatePollTest < Test::Unit::TestCase def setup - @poll = DatePoll.new - end - def teardown - File.delete("#{SITE}.yaml") if File.exists?("#{SITE}.yaml") + @poll = DatePoll.new(SITE, false) end def test_add_remove_column # how to test cgi class? @@ -0,0 +1,37 @@ +class Hash + def compare_by_values(other, fieldarray) + return 0 if fieldarray.size == 0 + return -1 if self[fieldarray[0]].nil? + return 1 if other[fieldarray[0]].nil? + + if self[fieldarray[0]] == other[fieldarray[0]] + if fieldarray.size == 1 + return 0 + else + return self.compare_by_values(other, fieldarray[1,fieldarray.size-1]) + end + else + self[fieldarray[0]] <=> other[fieldarray[0]] + end + end +end + +if __FILE__ == $0 +require "test/unit" + class Hash_test < Test::Unit::TestCase + def test_compare_by_values + a = {1 =>1, 2=>2, 3=>3} + b = {1 =>1, 2=>2, 3=>2} + c = {1 =>1, 2=>3, 3=>3} + d = {1 =>1, 2=>3} + + assert_equal( 0,a.compare_by_values(a,[1,2,3])) + assert_equal( 1,a.compare_by_values(b,[1,2,3])) + assert_equal(-1,a.compare_by_values(c,[1,2,3])) + assert_equal( 1,c.compare_by_values(d,[1,2,3])) + assert_equal(-1,d.compare_by_values(c,[1,2,3])) + assert_equal( 0,d.compare_by_values(c,[])) + end + end +end + @@ -1,3 +1,4 @@ +require "hash" require "yaml" class Poll @@ -10,14 +11,15 @@ class Poll @comment = [] store "Poll #{name} created" end - def sort_data field + def sort_data fields @data.sort{|x,y| - if field == "name" - x[0] <=> y[0] - elsif x[1][field].nil? or y[1][field].nil? - x[1][field].to_s <=> y[1][field].to_s + if fields.include?("name") + until fields.pop == "name" + end + cmp = x[1].compare_by_values(y[1],fields) == 0 + return cmp == 0 ? x[0] <=> y[0] : cmp else - x[1][field] <=> y[1][field] + return x[1].compare_by_values(y[1],fields) end } end @@ -37,7 +39,7 @@ class Poll ret += head_to_html - sort_data($cgi.include?("sort") ? $cgi["sort"] : "timestamp").each{|participant,poll| + sort_data($cgi.include?("sort") ? $cgi.params["sort"] : ["timestamp"]).each{|participant,poll| ret += "<tr>\n" ret += "<td class='name'>#{participant}</td>\n" @head.sort.each{|columntitle,columndescription| @@ -169,12 +171,13 @@ END @head.each_key{|columntitle| @data[htmlname][columntitle] = agreed[columntitle.to_s] } - store "Participant #{name} edited" + store "Participant #{name.strip} edited" end def invite_delete(name) - if @data.has_key?(name) - @data.delete(CGI.escapeHTML(name.strip)) - store "Participant #{name} deleted" + htmlname = CGI.escapeHTML(name.strip) + if @data.has_key?(htmlname) + @data.delete(htmlname) + store "Participant #{name.strip} deleted" else add_participant(name,{}) end @@ -188,7 +191,7 @@ 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","<br />")] + @comment << [Time.now, CGI.escapeHTML(name.strip), CGI.escapeHTML(comment.strip).gsub("\r\n","<br />")] store "Comment added by #{name}" end def delete_comment index @@ -212,18 +215,19 @@ end if __FILE__ == $0 require 'test/unit' +require 'cgi' +require 'pp' SITE = "glvhc_8nuv_8fchi09bb12a-23_uvc" class Poll attr_accessor :head, :data, :comment + def store comment + end end class PollTest < Test::Unit::TestCase def setup - @poll = Poll.new - end - def teardown - File.delete("#{SITE}.yaml") if File.exists?("#{SITE}.yaml") + @poll = Poll.new(SITE, false) end def test_init assert(@poll.head.empty?) @@ -234,21 +238,12 @@ class PollTest < Test::Unit::TestCase assert_equal(Time, @poll.data["bla"]["timestamp"].class) assert(@poll.data["bla"]["Item 2"]) end - def test_delete - @poll.data["bla"] = {} - @poll.delete(" bla ") + def test_invite_delete + @poll.invite_delete(" bla ") + assert_equal(Hash, @poll.data["bla"].class) + @poll.invite_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) |