=begin
(plugin/)sum_price_x.rb
nDiary Plug-in 家計簿的数値の作成
.priceファイルに付された金銭を合計します。
option:
list=on 一覧表示します。
total=on 全.priceファイル合計します。
month=on list=on,total=onで使用、月別表示も行う場合です。
default=val 値段不明時に加算する値を設定します。(無指定時500)
filter=on codeをISBNと見做して修正し、Filterを通します。
list,totalとは共存しませんし、defaultは無意味になります。
format:
.priseファイルは1行1件、TAB区切り
date, anchor, title, type, code, price
8桁日付, 日記アンカー, 品名, 品型(book,cd等), 型番(ISBN等), 価格
以上で十分だと思うのですが。
note:
そういう訳で、表示形式・品物別の集計等は適時改造して下さい。
exsample:
reference:
http://www.ruby-lang.org/ja/man-1.6/index.cgi?cmd=view;name=%C0%B5%B5%AC%C9%BD%B8%BD#a.a5.b5.a5.f3.a5.d7.a5.eb
update:
2003.11.4
=end
class String
def to_yen
newstr, count = '', 0
newstr = self.gsub(/(\d)((?:\d\d\d)+\b)/, '\1,\2')
newstr = '\\' + newstr
return newstr
end
end
def plug(opt)
default_price = 500
if opt.has_key?('default')
default_price = opt['default'].to_i
end
filelist = []
if opt['total'] == 'on'
#@logDirectory直下だけ探せばいいので、修正した方が効率がいいかも
Dir::find(@logDirectory){ |file|
next unless /\/\d{6}\.price$/i =~ file
filelist << file
}
else
if self.kind_of?(LastDiary) or self.kind_of?(Topics) then
thismonth = @diaries[0][0..5]
elsif self.kind_of?(PastDiary) then
thismonth = @targetMonth
# else
# return
end
filelist << @logDirectory + thismonth + '.price'
end
price, size, list = 0, 0, []
filelist.each{ |file|
lines = File.exists?(file) ? open(file).readlines : []
lines.each{ |line|
data = line.chomp.split("\t")
#データの書式
#date, anchor, title, type, code, price = line.split("\t")
val = data[-1].to_i
val = default_price if (val == 0) && (data[-1] !~ /0/)
data[-1] = val
price += val
list << data
}
size += lines.size
}
str = ''
yearmonth, price2, num = '', 0, 0
#ちょっとは見栄えよくしとくか
if opt['list'] == 'on'
list.each{ |line|
date, anchor, title, type, code, price1 = line
if opt['month'] == 'on'
if yearmonth != date[0..5]
if yearmonth != ''
str << '
' + ['', '', price2.to_s.to_yen + '円(' + num.to_s + '冊)'].join(' | ') + ' |
'
str << "<\/table>\n\n"
end
str << "#{Time::local(date[0..3], date[4..5]).strftime2('%Y年%m月')}
\n\n"
str << ''
yearmonth = date[0..5]
price2, num = 0, 0
end
end
if yearmonth == ''
str << ''
yearmonth = 'off'
end
if date =~ /(\d\d\d\d)(\d\d)(\d\d)/
datestr = "#{$1.to_i.to_s}\/#{$2.to_i.to_s}\/#{$3.to_i.to_s}"
end
str << '' + ["#{datestr}<\/a>", title, price1.to_s.to_yen].join(' | ') + ' |
'
price2 += price1.to_i
num += 1
}
if opt['month'] == 'on'
str << '' + ['', '', price2.to_s.to_yen + '円(' + num.to_s + '冊)'].join(' | ') + ' |
'
str << "<\/table>\n\n"
str << "合計
\n\n"
str << ''
end
str << '' + ['', '', price.to_s.to_yen + '円(' + size.to_s + '冊)'].join(' | ') + ' |
'
str << '
'
else
str << ''
str << price.to_s.to_yen + '円(' + size.to_s + '冊)'
str << '
'
end
if opt['filter'] == 'on'
str = ''
list.each{ |line|
date, anchor, title, type, code, price1 = line
bookstr = '(ISBN' + code + ')'
@filter.bookstore_x(bookstr, :P)
if date =~ /(\d\d\d\d)(\d\d)(\d\d)/
datestr = "#{$1.to_i.to_s}\/#{$2.to_i.to_s}\/#{$3.to_i.to_s}"
end
str << "'
str << bookstr.gsub(/\n/,"
\n")
}
end
return str
end