Class: Jota::MboxStorage

Inherits:
Storage show all
Defined in:
lib/mbox_storage-tmail.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(book, filename = nil) ⇒ MboxStorage

Returns a new instance of MboxStorage.



175
176
177
178
179
# File 'lib/mbox_storage-tmail.rb', line 175

def initialize book, filename=nil
  @book = book
  @filename = File.expand_path filename
  @is_rw = true
end

Instance Attribute Details

#bookObject (readonly)

Returns the value of attribute book.



173
174
175
# File 'lib/mbox_storage-tmail.rb', line 173

def book
  @book
end

#filenameObject (readonly)

Returns the value of attribute filename.



173
174
175
# File 'lib/mbox_storage-tmail.rb', line 173

def filename
  @filename
end

#is_rwObject (readonly)

Returns the value of attribute is_rw.



173
174
175
# File 'lib/mbox_storage-tmail.rb', line 173

def is_rw
  @is_rw
end

Class Method Details

.load(filename, mode = :rw) ⇒ Object



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
# File 'lib/mbox_storage-tmail.rb', line 181

def self.load filename, mode = :rw
  Log.info "loading Mbox file '#{filename}' with mode '#{mode}'"
  array = []
  pref = nil
  TMail::UNIXMbox.new(filename,nil,true).each_port do |port|
    mail = TMail::Mail.new(port)
    Log.debug "MboxStorage.load: '#{mail.inspect}'"
    type = parse_type(mail.content_type)
    data = mail.body
    hash = {
            "created" => Date.parse(mail["created-at"]),
            "data" => data,
            "title" => mail.subject,
            "type" => type,
            "wrap" => mail["flags"].downcase.include?("w"),
           }
    if type == :pref then
      pref = YAML.load data
    else
      page = Page.from_hash hash
      array << page
    end
  end

  storage = self.new (Book.from_array(array, pref)), filename
  return storage
end

Instance Method Details

#saveObject



240
241
242
# File 'lib/mbox_storage-tmail.rb', line 240

def save
  save_as @filename if @filename
end

#save_as(filename) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/mbox_storage-tmail.rb', line 209

def save_as filename
  return if ! @book
  Log.info "saving Mbox file '#{filename}'"
  # mailbox = UNIXMbox.new(filename,nil,true)
  array = @book.map{|page|page.to_hash}

  File.open(@filename, "w")  do |file|
    # preferences
    mail = TMail::Mail.new
    mail["created-at"] = Time.now.to_s
    mail["flags"] = ""
    mail.subject = "Preferences"
    mail.body = YAML.dump @book.pref
    mail.set_content_type "application", "x-jota-pref"
    Log.debug "MboxStorage.save_as: pref '#{mail.inspect}'"
    file.print mail.encoded

    # pages
    array.each do |hash|
      mail = TMail::Mail.new
      mail["created-at"] = hash["created"].to_s
      mail["flags"] = hash["wrap"]? "w" : "" # the only flag for now
      mail.subject = hash["title"]
      mail.body = hash["data"]
      mail.set_content_type "text", "plain" # all non-:pref pages are :text for now
      Log.debug "MboxStorage.save_as: page '#{mail.inspect}'"
      file.print mail.encoded
    end
  end
end