class MingleEvents::EntryCache

Public Class Methods

new(root_dir) click to toggle source
  # File lib/mingle_events/entry_cache.rb
3 def initialize(root_dir)
4   @dir = ZipDirectory.new(root_dir)
5 end

Public Instance Methods

all_entries() click to toggle source
   # File lib/mingle_events/entry_cache.rb
 7 def all_entries
 8   current_state = load_current_state
 9   Entries.new(@dir, current_state[:first_fetched_entry_info_file], current_state[:last_fetched_entry_info_file])
10 end
clear() click to toggle source
   # File lib/mingle_events/entry_cache.rb
50 def clear
51   @dir.delete
52 end
entries(from_entry, to_entry) click to toggle source
   # File lib/mingle_events/entry_cache.rb
12 def entries(from_entry, to_entry)
13   Entries.new(@dir, file_for_entry(from_entry), file_for_entry(to_entry))
14 end
first() click to toggle source
   # File lib/mingle_events/entry_cache.rb
16 def first
17   current_state_entry(:first_fetched_entry_info_file)
18 end
has_current_state?() click to toggle source
   # File lib/mingle_events/entry_cache.rb
30 def has_current_state?
31   @dir.exists?(current_state_file)
32 end
latest() click to toggle source
   # File lib/mingle_events/entry_cache.rb
20 def latest
21   current_state_entry(:last_fetched_entry_info_file)
22 end
set_current_state(latest_entry) click to toggle source
   # File lib/mingle_events/entry_cache.rb
34 def set_current_state(latest_entry)
35   return if latest_entry.nil?
36   write(latest_entry, nil)
37   update_current_state(latest_entry, latest_entry)
38 end
update_current_state(oldest_new_entry, most_recent_new_entry) click to toggle source
   # File lib/mingle_events/entry_cache.rb
40 def update_current_state(oldest_new_entry, most_recent_new_entry)
41   current_state = load_current_state
42   current_state.merge!(:last_fetched_entry_info_file => file_for_entry(most_recent_new_entry))
43   if current_state[:first_fetched_entry_info_file].nil?
44     current_state.merge!(:first_fetched_entry_info_file => file_for_entry(oldest_new_entry))
45   end
46   @dir.write_file(current_state_file) { |out| YAML.dump(current_state, out)  }
47   @dir.reload
48 end
write(entry, next_entry) click to toggle source
   # File lib/mingle_events/entry_cache.rb
24 def write(entry, next_entry)
25   file = file_for_entry(entry)
26   file_content = {:entry_xml => entry.raw_xml, :next_entry_file_path => file_for_entry(next_entry)}
27   @dir.write_file(file) {|out| YAML.dump(file_content, out)}
28 end

Private Instance Methods

current_state_entry(info_file_key) click to toggle source
   # File lib/mingle_events/entry_cache.rb
68 def current_state_entry(info_file_key)
69   if info_file = load_current_state[info_file_key]
70     Feed::Entry.from_snippet((@dir.file(info_file) { |f| YAML.load(f) })[:entry_xml])
71   end
72 end
current_state_file() click to toggle source
   # File lib/mingle_events/entry_cache.rb
64 def current_state_file
65   'current_state.yml'
66 end
file_for_entry(entry) click to toggle source
   # File lib/mingle_events/entry_cache.rb
74 def file_for_entry(entry)
75   return nil if entry.nil?
76   entry_id_as_uri = URI.parse(entry.entry_id)
77   relative_path_parts = entry_id_as_uri.path.split('/').reject(&:blank?)
78   entry_id_int = relative_path_parts.last
79   insertions = ["#{entry_id_int.to_i/16384}", "#{entry_id_int.to_i%16384}"]
80   relative_path_parts = relative_path_parts[0..-2] + insertions + ["#{entry_id_int}.yml"]  
81   File.join(*relative_path_parts)
82 end
load_current_state() click to toggle source
   # File lib/mingle_events/entry_cache.rb
56 def load_current_state
57   if has_current_state?
58     @dir.file(current_state_file) { |f| YAML.load(f)}
59   else
60     {:last_fetched_entry_info_file => nil, :first_fetched_entry_info_file => nil}
61   end
62 end