class Mhc::DataStore
Public Class Methods
new(basedir)
click to toggle source
# File lib/mhc/datastore.rb, line 6 def initialize(basedir) unless basedir and File.directory?(File.expand_path(basedir.to_s)) raise Mhc::ConfigurationError, "datastore directory '#{basedir}' not found" end @basedir = Pathname.new(File.expand_path(basedir)) @cache = Cache.new(File.expand_path("status/cache/events.pstore", @basedir)) end
Public Instance Methods
create(event)
click to toggle source
# File lib/mhc/datastore.rb, line 55 def create(event) if find_by_uid(event.uid) raise "Already exist uid:#{uid} in #{@basedir}" end File.open(path, "w") do |f| f.write(event.dump) end end
delete(uid_or_event)
click to toggle source
# File lib/mhc/datastore.rb, line 73 def delete(uid_or_event) uid = if uid_or_event.respond_to?(:uid) uid_or_event.uid else uid_or_event end if path = find_path(uid) File.delete(path) else raise "Not found uid:#{uid} in #{@basedir}" end end
each_cache_entry() { |uid, ent| ... }
click to toggle source
dump cache entry for debug usage
# File lib/mhc/datastore.rb, line 87 def each_cache_entry @cache.each do |uid, ent| yield uid, ent end end
entries(range: nil, category: nil, recurrence: nil)
click to toggle source
# File lib/mhc/datastore.rb, line 14 def entries(range: nil, category: nil, recurrence: nil) if range int_range = range.min.absolute_from_epoch .. range.max.absolute_from_epoch end Enumerator.new do |yielder| ["inbox", "spool", "presets"].each do |slot| dir = File.expand_path(slot, @basedir) next unless File.directory?(dir) Dir.chdir(dir) do Dir.foreach(".") do |ent| parse_mhcc(ent).each {|ev| next if category && !ev.in_category?(category) next if recurrence && !ev.in_recurrence?(recurrence) yielder << ev } if /\.mhcc$/ =~ ent next unless /\.mhc$/ =~ ent uid = $` cache_entry = @cache.lookup(uid, ent) next if range && !cache_entry.in_range?(int_range) next if category && !cache_entry.in_category?(category) next if recurrence && !cache_entry.in_recurrence?(recurrence) yielder << Event.parse_file(File.expand_path(ent)) end end end @cache.save end end
find_by_uid(uid)
click to toggle source
# File lib/mhc/datastore.rb, line 49 def find_by_uid(uid) path = find_path(uid) return nil unless path return Event.parse_file(path) end
logger()
click to toggle source
# File lib/mhc/datastore.rb, line 45 def logger @logger ||= Mhc::Logger.new(@logfile) end
update(event)
click to toggle source
# File lib/mhc/datastore.rb, line 64 def update(event) unless path = uid_to_path(event.uid) raise "Not found uid:#{uid} in #{@basedir}" end File.open(path, "w") do |f| f.write(event.dump) end end
Private Instance Methods
find_path(uid)
click to toggle source
# File lib/mhc/datastore.rb, line 103 def find_path(uid) glob = @basedir + ('**/' + uid + '.mhc') return Dir.glob(glob).first end
parse_mhcc(filename)
click to toggle source
# File lib/mhc/datastore.rb, line 96 def parse_mhcc(filename) string = File.open(filename).read.scrub.gsub(/^\s*#.*$/, "").strip string.split(/\n\n\n*/).map do |header| Event.parse(header) end end
uid_to_path(uid)
click to toggle source
# File lib/mhc/datastore.rb, line 108 def uid_to_path(uid) return @basedir + ('spool/' + uid + '.mhc') end