Class: TDiary::IO::Default

Inherits:
Base
  • Object
show all
Includes:
Cache, Comment, Referer
Defined in:
lib/tdiary/io/default.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Cache

#clear_cache, #restore_cache, #store_cache

Methods included from Referer

#referer_file, #restore_referer, #store_referer

Methods included from Comment

#comment_file, #restore_comment, #store_comment

Methods inherited from Base

#cache_path, #diary_factory, #initialize, #load_styles, #style

Constructor Details

This class inherits a constructor from TDiary::IO::Base

Class Method Details

.load_cgi_conf(conf) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/tdiary/io/default.rb', line 145

def load_cgi_conf(conf)
  conf.class.class_eval { attr_accessor :data_path }
  raise TDiaryError, 'No @data_path variable.' unless conf.data_path

  conf.data_path += '/' if /\/$/ !~ conf.data_path
  raise TDiaryError, 'Do not set @data_path as same as tDiary system directory.' if conf.data_path == "#{TDiary::PATH}/"

  File::open( "#{conf.data_path}tdiary.conf" ){|f| f.read }
rescue IOError, Errno::ENOENT
end

.parse_tdiary(data) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/tdiary/io/default.rb', line 127

def parse_tdiary( data )
  header, body = data.split( /\r?\n\r?\n/, 2 )
  headers = {}
  if header then
    header.lines.each do |l|
      l.chomp!
      key, val = l.scan( /([^:]*):\s*(.*)/ )[0]
      headers[key] = val ? val.chomp : nil
    end
  end
  if body then
    body.gsub!( /^\./, '' )
  else
    body = ''
  end
  [headers, body]
end

.save_cgi_conf(conf, result) ⇒ Object



156
157
158
159
# File 'lib/tdiary/io/default.rb', line 156

def save_cgi_conf(conf, result)
  File::open( "#{conf.data_path}tdiary.conf", 'w' ) {|o| o.print result }
rescue IOError, Errno::ENOENT
end

Instance Method Details

#cache_dirObject



232
233
234
# File 'lib/tdiary/io/default.rb', line 232

def cache_dir
  @tdiary.conf.cache_path || "#{@data_path}/cache"
end

#calendarObject



221
222
223
224
225
226
227
228
229
230
# File 'lib/tdiary/io/default.rb', line 221

def calendar
  calendar = {}
  Dir["#{@data_path}????/??????.td2"].sort.each do |file|
    if file =~ /(\d{4})(\d{2})\.td2$/
      calendar[$1] = [] unless calendar[$1]
      calendar[$1] << $2
    end
  end
  calendar
end

#transaction(date) ⇒ Object

block must be return boolean which dirty diaries.



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/tdiary/io/default.rb', line 165

def transaction( date )
  diaries = {}
  dir = date.strftime( "#{@data_path}%Y" )
  @dfile = date.strftime( "#{@data_path}%Y/%Y%m.td2" )
  cfile = comment_file( @data_path, date )
  rfile = referer_file( @data_path, date )
  begin
    FileUtils.mkdir_p(dir)
    begin
      fh = File::open( @dfile, 'r+' )
    rescue
      fh = File::open( @dfile, 'w+' )
    end
    fh.flock( File::LOCK_EX )

    cache = restore_parser_cache( date, 'default' )
    force_save = TDiaryBase::DIRTY_NONE
    unless cache then
      force_save |= restore( fh, diaries )
      force_save |= restore_comment( cfile, diaries )
      force_save |= restore_referer( rfile, diaries )
    else
      diaries.update( cache )
    end
    dirty = yield( diaries ) if block_given?
    store( fh, diaries ) if ((dirty | force_save) & TDiaryBase::DIRTY_DIARY) != 0
    store_comment( cfile, diaries ) if ((dirty | force_save) & TDiaryBase::DIRTY_COMMENT) != 0
    store_referer( rfile, diaries ) if ((dirty | force_save) & TDiaryBase::DIRTY_REFERER) != 0
    if dirty != TDiaryBase::DIRTY_NONE or not cache then
      store_parser_cache(date, diaries, 'default')
    end

    if diaries.empty?
      begin
        if fh then
          fh.close
          fh = nil
        end
        File::delete( @dfile )
      rescue Errno::ENOENT
      end
      begin
        store_parser_cache(date, nil, nil)
      rescue Errno::ENOENT
      end
    end
    # delete dispensable data directory
    begin
      Dir.delete( dir ) if Dir.new( dir ).entries.reject {|f| "." == f or ".." == f}.empty?
    rescue Errno::ENOENT
    end
  ensure
    fh.close if fh
  end
end