Skip to content

Commit

Permalink
support plugin storage in category plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
tdtds committed Nov 14, 2014
1 parent 84b789c commit 9cd35b4
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions misc/plugin/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# Copyright (c) 2003 Junichiro KITA <kita@kitaj.no-ip.com>
# Distributed under the GPL2 or any later version.
#
require 'pstore'

#
# initialize
Expand Down Expand Up @@ -419,8 +418,16 @@ class Cache
def initialize(conf, bind)
@conf = conf
@binding = bind # ...... very ugly
@dir = "#{conf.data_path}/category"
Dir.mkdir(@dir) unless File.exist?(@dir)
@plugin = @binding.eval('self')
@categories = nil
end

def get(db, cat)
JSON.load(db.get(cat))
end

def set(db, cat, data)
db.set(cat, data.to_json)
end

def add_categories(list)
Expand All @@ -429,22 +436,15 @@ def add_categories(list)
end

def replace_categories(list)
PStore.new(cache_file).transaction do |db|
db['category'] = list.sort.uniq
end
@categories = list
end

#
# restore category names
# ["category1", "category2", ...]
#
def restore_categories
list = nil
PStore.new(cache_file).transaction do |db|
list = db['category'] if db.root?('category')
db.abort
return @categories if @categories
@plugin.__send__(:transaction, 'category') do |db|
@categories = db.keys
end
list || []
return @categories
end

#
Expand All @@ -459,24 +459,25 @@ def replace_sections(diary)
deleted = []
ymd = diary.date.strftime('%Y%m%d')

categories.each do |c|
PStore.new(cache_file(c)).transaction do |db|
db['category'] = {} unless db.root?('category')
@plugin.__send__(:transaction, 'category') do |db|
categories.each do |c|
cat = get(db, c) || {}
if diary.visible? and categorized[c]
db['category'].update(categorized[c])
cat.update(categorized[c])
set(db, c, cat)
else
# diary is invisible or sections of this category is deleted
db['category'].delete(ymd)
deleted << c if db['category'].empty?
cat.delete(ymd)
if cat.empty?
db.delete(c)
deleted << c
end
end
end
end

if !deleted.empty?
deleted.each do |c|
File.unlink(cache_file(c))
if !deleted.empty?
replace_categories(categories - deleted)
end
replace_categories(categories - deleted)
end
end

Expand Down Expand Up @@ -522,9 +523,8 @@ def categorize(category, years)
begin
categorized.clear
categories.each do |c|
PStore.new(cache_file(c)).transaction do |db|
categorized[c] = db['category']
db.abort
@plugin.__send__(:transaction, 'category') do |db|
categorized[c] = get(db, c)
end
categorized[c].keys.each do |ymd|
y, m = ymd[0,4], ymd[4,2]
Expand Down Expand Up @@ -591,10 +591,10 @@ def initial_replace_sections(diary)
return if diary.nil? or !diary.visible? or !diary.categorizable?

categorized = categorize_diary(diary)
categorized.keys.each do |c|
PStore.new(cache_file(c)).transaction do |db|
db['category'] = {} unless db.root?('category')
db['category'].update(categorized[c])
@plugin.__send__(:transaction, 'category') do |db|
categorized.keys.each do |c|
cat = get(db, c) || {}
set(db, c, cat.update(categorized[c]))
end
end
end
Expand Down

0 comments on commit 9cd35b4

Please sign in to comment.