class Diary::Store

Public Class Methods

new(fname=nil) click to toggle source
# File lib/diary-ruby/store.rb, line 9
def initialize(fname=nil)
  fname ||= DEFAULT_FILENAME

  @path = fname
  @file = PStore.new(fname)
end

Public Instance Methods

[](key) click to toggle source
# File lib/diary-ruby/store.rb, line 55
def [](key)
  read(key)
end
path() click to toggle source
# File lib/diary-ruby/store.rb, line 16
def path
  @path
end
read(key) click to toggle source
# File lib/diary-ruby/store.rb, line 59
def read(key)
  out = nil
  @file.transaction(true) do |db|
    out = db[key]
  end
  out
end
update_db_timestamp(db) click to toggle source
# File lib/diary-ruby/store.rb, line 51
def update_db_timestamp(db)
  db[:last_update_at] = Time.now.utc.strftime('%c')
end
write() { |db| ... } click to toggle source
# File lib/diary-ruby/store.rb, line 44
def write
  @file.transaction do |db|
    yield db
    update_db_timestamp(db)
  end
end
write_entry(entry) click to toggle source
# File lib/diary-ruby/store.rb, line 20
def write_entry(entry)
  Diary.debug("WRITING ENTRY #{ entry.to_hash }")

  @file.transaction do |db|
    # entries index
    db[:entries] ||= []
    db[:entries] << entry.key unless db[:entries].include?(entry.key)

    # actual entry
    db[entry.key] = entry.to_hash.merge(
      updated_at: Time.now.utc.strftime('%c')
    )

    # reverse tags index (from tag to entries)
    db[:tags] ||= {}
    (entry.tags || []).each do |tag|
      db[:tags][tag] ||= []
      db[:tags][tag] = (db[:tags][tag] + [entry.key]).uniq
    end

    update_db_timestamp(db)
  end
end