Module: TDiary::IO::Comment

Included in:
Default
Defined in:
lib/tdiary/io/default.rb

Instance Method Summary collapse

Instance Method Details

#comment_file(data_path, date) ⇒ Object



18
19
20
# File 'lib/tdiary/io/default.rb', line 18

def comment_file( data_path, date )
  date.strftime( "#{data_path}%Y/%Y%m.tdc" )
end

#restore_comment(file, diaries) ⇒ Object



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

def restore_comment( file, diaries )
  minor = ''
  begin
    File::open( file ) do |fh|
      fh.flock( File::LOCK_SH )

      major, minor = fh.gets.chomp.split( /\./, 2 )
      unless TDIARY_MAGIC_MAJOR == major then
        raise StandardError, 'bad file format.'
      end

      s = fh.read
      s = migrate_to_01( s ) if minor == '00.00' and !@tdiary.conf['stop_migrate_01']
      s.split( /\r?\n\.\r?\n/ ).each do |l|
        headers, body = Default.parse_tdiary( l )
        next unless body
        comment = ::TDiary::Comment::new(
          headers['Name'],
          headers['Mail'],
          body,
          Time::at( headers['Last-Modified'].to_i ) )
        comment.show = false if headers['Visible'] == 'false'
        diaries[headers['Date']].add_comment( comment ) if headers['Date']
      end
    end
  rescue Errno::ENOENT
  end
  return minor == '00.00' ? TDiaryBase::DIRTY_COMMENT : TDiaryBase::DIRTY_NONE
end

#store_comment(file, diaries) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/tdiary/io/default.rb', line 52

def store_comment( file, diaries )
  File::open( file, File::WRONLY | File::CREAT ) do |fhc|
    fhc.flock( File::LOCK_EX )
    fhc.rewind
    fhc.truncate( 0 )
    fhc.puts( TDIARY_MAGIC )
    diaries.each do |date,diary|
      diary.each_comment( diary.count_comments( true ) ) do |com|
        fhc.puts( "Date: #{date}" )
        fhc.puts( "Name: #{com.name}" )
        fhc.puts( "Mail: #{com.mail}" )
        fhc.puts( "Last-Modified: #{com.date.to_i}" )
        fhc.puts( "Visible: #{com.visible? ? 'true' : 'false'}" )
        fhc.puts
        fhc.puts( com.body.gsub( /\r/, '' ).sub( /\n+\Z/, '' ).gsub( /\n\./, "\n.." ) )
        fhc.puts( '.' )
      end
    end
  end
end