Class: TDiary::IO::PStore

Inherits:
Object show all
Defined in:
lib/tdiary/io/pstore.rb

Instance Method Summary collapse

Constructor Details

#initialize(tdiary) ⇒ PStore

Returns a new instance of PStore.



13
14
15
# File 'lib/tdiary/io/pstore.rb', line 13

def initialize( tdiary )
  @data_path = tdiary.conf.data_path
end

Instance Method Details

#calendarObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/tdiary/io/pstore.rb', line 46

def calendar
  calendar = {}
  Dir["#{@data_path}??????"].sort.each do |file|
    year, month = file.scan( %r[/(\d{4})(\d\d)$] )[0]
    next unless year
    calendar[year] = [] unless calendar[year]
    calendar[year] << month
  end
  calendar
end

#diary_factory(date, title, body, style = nil) ⇒ Object



57
58
59
# File 'lib/tdiary/io/pstore.rb', line 57

def diary_factory( date, title, body, style = nil )
  Diary::new( date, title, body )
end

#transaction(date) ⇒ Object

block must be return boolean which dirty diaries.



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
# File 'lib/tdiary/io/pstore.rb', line 20

def transaction( date )
  diaries = {}
  filename = date.strftime( "#{@data_path}%Y%m" )
  begin
    PStore::new( filename ).transaction do |db|
      dirty = false
      if db.root?( 'diary' ) then
        diaries.update( db['diary'] )
      end
      dirty = yield( diaries ) if iterator?
      if dirty != TDiary::TDiaryBase::DIRTY_NONE then
        db['diary'] = diaries
      else
        db.abort
      end
    end
  rescue PStore::Error, NameError, Errno::EACCES
    raise PermissionError::new( "make your @data_path to writable via httpd. #$!" )
  end
  begin
    File::delete( filename ) if diaries.empty?
  rescue Errno::ENOENT
  end
  return diaries
end