Class: MingleEvents::EntryCache

Inherits:
Object
  • Object
show all
Defined in:
lib/mingle_events/entry_cache.rb

Defined Under Namespace

Classes: Entries

Instance Method Summary collapse

Constructor Details

#initialize(root_dir) ⇒ EntryCache

Returns a new instance of EntryCache.



3
4
5
# File 'lib/mingle_events/entry_cache.rb', line 3

def initialize(root_dir)
  @dir = ZipDirectory.new(root_dir)
end

Instance Method Details

#all_entriesObject



7
8
9
10
# File 'lib/mingle_events/entry_cache.rb', line 7

def all_entries
  current_state = load_current_state
  Entries.new(@dir, current_state[:first_fetched_entry_info_file], current_state[:last_fetched_entry_info_file])
end

#clearObject



50
51
52
# File 'lib/mingle_events/entry_cache.rb', line 50

def clear
  @dir.delete
end

#entries(from_entry, to_entry) ⇒ Object



12
13
14
# File 'lib/mingle_events/entry_cache.rb', line 12

def entries(from_entry, to_entry)
  Entries.new(@dir, file_for_entry(from_entry), file_for_entry(to_entry))
end

#firstObject



16
17
18
# File 'lib/mingle_events/entry_cache.rb', line 16

def first
  current_state_entry(:first_fetched_entry_info_file)
end

#has_current_state?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/mingle_events/entry_cache.rb', line 30

def has_current_state?
  @dir.exists?(current_state_file)
end

#latestObject



20
21
22
# File 'lib/mingle_events/entry_cache.rb', line 20

def latest
  current_state_entry(:last_fetched_entry_info_file)
end

#set_current_state(latest_entry) ⇒ Object



34
35
36
37
38
# File 'lib/mingle_events/entry_cache.rb', line 34

def set_current_state(latest_entry)
  return if latest_entry.nil?
  write(latest_entry, nil)
  update_current_state(latest_entry, latest_entry)
end

#update_current_state(oldest_new_entry, most_recent_new_entry) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/mingle_events/entry_cache.rb', line 40

def update_current_state(oldest_new_entry, most_recent_new_entry)
  current_state = load_current_state
  current_state.merge!(:last_fetched_entry_info_file => file_for_entry(most_recent_new_entry))
  if current_state[:first_fetched_entry_info_file].nil?
    current_state.merge!(:first_fetched_entry_info_file => file_for_entry(oldest_new_entry))
  end
  @dir.write_file(current_state_file) { |out| YAML.dump(current_state, out)  }
  @dir.reload
end

#write(entry, next_entry) ⇒ Object



24
25
26
27
28
# File 'lib/mingle_events/entry_cache.rb', line 24

def write(entry, next_entry)
  file = file_for_entry(entry)
  file_content = {:entry_xml => entry.raw_xml, :next_entry_file_path => file_for_entry(next_entry)}
  @dir.write_file(file) {|out| YAML.dump(file_content, out)}
end