Class: Poro::Contexts::HashContext

Inherits:
Poro::Context show all
Defined in:
lib/poro/contexts/hash_context.rb

Overview

Not a practical real world context manager, this is a simple in-memory store that uses a normal Ruby Hash. The intended use for this context is for building and testing code before a more realistic persistence backing is available for your application.

Instance Attribute Summary

Attributes inherited from Poro::Context

#data_store, #klass, #primary_key

Instance Method Summary collapse

Methods inherited from Poro::Context

configure_for_klass, factory, factory=, fetch, managed_class?, #primary_key_value, #set_primary_key_value

Methods included from Poro::Context::FindMethods

#data_store_find, #find, included

Constructor Details

#initialize(klass) ⇒ HashContext

Returns a new instance of HashContext.



9
10
11
12
# File 'lib/poro/contexts/hash_context.rb', line 9

def initialize(klass)
  self.data_store = {}
  super(klass)
end

Instance Method Details

#convert_to_data(obj) ⇒ Object



41
42
43
# File 'lib/poro/contexts/hash_context.rb', line 41

def convert_to_data(obj)
  return obj
end

#convert_to_plain_object(data) ⇒ Object



37
38
39
# File 'lib/poro/contexts/hash_context.rb', line 37

def convert_to_plain_object(data)
  return data
end

#fetch(id) ⇒ Object



14
15
16
# File 'lib/poro/contexts/hash_context.rb', line 14

def fetch(id)
  return convert_to_plain_object( data_store[clean_id(id)] )
end

#remove(obj) ⇒ Object

Remove the object from the underlying hash.



28
29
30
31
32
33
34
35
# File 'lib/poro/contexts/hash_context.rb', line 28

def remove(obj)
  pk_id = self.primary_key_value(obj)
  if( pk_id != nil )
    data_store.delete(obj.id)
    self.set_primary_key_value(obj, nil)
  end
  return self
end

#save(obj) ⇒ Object

Save the object in the underlying hash, using the object id as the key.



19
20
21
22
23
24
25
# File 'lib/poro/contexts/hash_context.rb', line 19

def save(obj)
  pk_id = self.primary_key_value(obj)
  self.set_primary_key_value(obj, obj.object_id) if(pk_id.nil?)
  
  data_store[obj.id] = convert_to_data(obj)
  return self
end