Class: HistoryBook::Memory::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/history_book/memory/store.rb

Constant Summary collapse

@@events =
{}
@@events_lock =
Mutex.new

Instance Method Summary collapse

Instance Method Details

#_filter_events(events, from, to) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/history_book/memory/store.rb', line 23

def _filter_events(events, from, to)
  if to
    events = events.reject.with_index { |e, i| i > to - 1 }
  end

  events.select.with_index { |e, i| i >= from - 1 }
end

#load_events(id, options = {}) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/history_book/memory/store.rb', line 7

def load_events(id, options = {})
  options = {:from => 1, :to => nil}.merge(options)
  from, to = options.values_at(:from, :to)

  events = @@events.fetch(id, [])
  _filter_events(events, from, to)
end

#store_events(id, new_events) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/history_book/memory/store.rb', line 15

def store_events(id, new_events)
  new_events = Array(new_events)
  @@events_lock.synchronize do
    @@events[id] ||= []
    @@events[id].concat(new_events)
  end
end