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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
#!/usr/bin/ruby
require 'amrita/template'
class Item
include Amrita::ExpandByMember
attr_reader :group, :name, :url
def initialize(group, name, url)
@group, @name, @url = group, name, url
end
def to_s
%Q[#{@group}|#{@name}|#{@url}]
end
def link
e(:a, :href=>@url) { @url }
end
end
class BookmarkList
attr_reader :groups
def initialize
@groups = {}
end
def load_from_file(path)
File::open(path) do |f|
f.each do |line|
begin
add_new_item(*line.chomp.split('|'))
rescue
end
end
end
end
def save_to_file(path)
File::open(path, "w") do |f|
@groups.each do |k, v|
v.each do |data|
f.puts data.to_s
end
end
end
end
def add_new_item(group="", name="", url="", *x)
item = Item.new(group, name, url)
@groups[group] ||= []
@groups[group] << item
end
end
if __FILE__ == $0
require 'runit/testcase'
require 'runit/cui/testrunner'
class TestBMModel < RUNIT::TestCase
def test_item
item = Item.new("aa", "bb", "http://www.xxx.com/")
assert_equal("aa", item.group)
assert_equal("bb", item.name)
assert_equal("http://www.xxx.com/", item.url)
end
def test_bookmarkmodel
bm = BookmarkList.new
assert_equal(0, bm.groups.size())
assert_equal({}, bm.groups)
bm.add_new_item("g", "nm", "http://www.xxx.com/")
assert_equal(1, bm.groups.size())
assert_equal(1, bm.groups["g"].size())
assert_equal("nm", bm.groups["g"][0].name)
assert_equal("http://www.xxx.com/", bm.groups["g"][0].url)
end
def test_load
bm = BookmarkList.new
bm.load_from_file("bookmark.dat.sample")
assert_equal(3, bm.groups.size())
assert_equal(3, bm.groups["BBS"].size())
assert_equal("2ch", bm.groups["BBS"][0].name)
assert_equal("http://www.ruby-lang.org/", bm.groups["Script Languages"][0].url)
end
def test_save
tmp = "/tmp/bmtest#{$$}"
bm = BookmarkList.new
bm.load_from_file("bookmark.dat.sample")
bm.add_new_item("html", "amrita", "http://kari.to/amrita/")
assert_equal(4, bm.groups.size())
assert_equal(3, bm.groups["BBS"].size())
assert_equal("2ch", bm.groups["BBS"][0].name)
assert_equal("http://www.ruby-lang.org/", bm.groups["Script Languages"][0].url)
assert_equal(1, bm.groups["html"].size())
assert_equal("amrita", bm.groups["html"][0].name)
bm.save_to_file(tmp)
bm = BookmarkList.new
bm.load_from_file(tmp)
assert_equal(4, bm.groups.size())
assert_equal(3, bm.groups["BBS"].size())
assert_equal("2ch", bm.groups["BBS"][0].name)
assert_equal("http://www.ruby-lang.org/", bm.groups["Script Languages"][0].url)
assert_equal(1, bm.groups["html"].size())
assert_equal("amrita", bm.groups["html"][0].name)
ensure
File::unlink tmp
end
end
if ARGV.size == 0
RUNIT::CUI::TestRunner.run(TestBMModel.suite)
else
ARGV.each do |method|
RUNIT::CUI::TestRunner.run(TestBMModel.new(method))
end
end
end
|