lib/asynchronic/data_store/in_memory.rb in asynchronic-0.1.0 vs lib/asynchronic/data_store/in_memory.rb in asynchronic-0.2.0

- old
+ new

@@ -1,46 +1,42 @@ module Asynchronic module DataStore class InMemory - def initialize + include Helper + + def initialize(hash={}) @hash = {} @mutex = Mutex.new + self.class.connections[object_id] = self end - def get(key) + def [](key) @hash[key.to_s] end - def set(key, value) + def []=(key, value) @mutex.synchronize { @hash[key.to_s] = value } end - def merge(key, hash) - scoped_key = Key.new key - hash.each do |k,v| - set scoped_key[k].to_s, v - end + def delete(key) + @hash.delete key.to_s end - def to_hash(key) - children_key = "#{key}:" - keys(children_key).inject({}) do |hash, k| - hash[k[children_key.size..-1]] = get k - hash - end + def keys + @hash.keys.map { |k| Key.new k } end - def keys(key=nil) - key ? keys.select { |k| k.start_with? key.to_s } : @hash.keys + alias_method :connection, :object_id + + def self.connect(object_id) + connections[object_id] end - def clear(key=nil) - if key - @hash.delete_if { |k,v| k.start_with? key.to_s } - else - @hash.clear - end + private + + def self.connections + @connections ||= {} end end end end \ No newline at end of file