Created
November 30, 2016 19:02
-
-
Save kylewelsby/bb9a68cf3ed5f733a11916838e804401 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'byebug' | |
require 'active_support/inflector' | |
module Sequares | |
module Store | |
class Memory < Base | |
class << self | |
def cache_key_for(klass, id) | |
inst = klass.with_history(id, []) | |
"#{inst.uri}|#{histories}" | |
end | |
end | |
attr_accessor :histories, :locks | |
def initialize | |
@histories = Hash.new { |hash, key| hash[key] = [] } | |
@locks = Hash.new { |hash, key| hash[key] = Mutex.new } | |
end | |
def filter_events(*klasses) | |
events = Hash.new | |
# NOTE: look into https://ruby-doc.org/core-2.0.0/Enumerator/Lazy.html | |
@histories.each do |key, value| | |
value.each do |event| | |
key_split = key.split('|') | |
klass = ActiveSupport::Inflector.classify(key_split.first).constantize.new(key_split.last) | |
events[klass] = event | |
end | |
end | |
events.sort do |a, b| | |
a.last.occurred_at <=> b.last.occurred_at | |
end.bsearch do |key, value| | |
klasses.flatten.any? do |klass| | |
value.is_a?(klass) || value.class.to_s.split("::").first.eql?(klass.to_s) | |
end | |
end | |
end | |
def cache_key_for(klass, id) | |
inst = klass.with_history(id, []) | |
[inst.uri, histories[inst.uri].length].join('#') | |
end | |
def save_history_for_aggregate(obj) | |
histories[obj.uri] = obj.history | |
end | |
def fetch_history_for_aggregate(obj) | |
histories[obj.uri] | |
end | |
def lock(obj) | |
raise ::Sequares::Entity::AlreadyLocked unless locks[obj.uri].try_lock | |
end | |
def unlock(obj) | |
locks[obj.uri].unlock | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment