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_class, factory, 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



44
45
46
# File 'lib/poro/contexts/hash_context.rb', line 44

def convert_to_data(obj)
  return obj
end

#convert_to_plain_object(data) ⇒ Object



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

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.



31
32
33
34
35
36
37
38
# File 'lib/poro/contexts/hash_context.rb', line 31

def remove(obj)
  pk_id = self.primary_key_value(obj)
  if( pk_id != nil )
    data_store.delete(pk_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
26
27
28
# File 'lib/poro/contexts/hash_context.rb', line 19

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