Module: Persistent
- Defined in:
- lib/persistent.rb,
lib/persistent/store.rb
Defined Under Namespace
Modules: ClassMethods Classes: Store
Class Method Summary collapse
-
.included(base) ⇒ Object
This module gives you objects:.
Instance Method Summary collapse
Class Method Details
.included(base) ⇒ Object
This module gives you objects:
class Person
include Persistent
attr_accessor :name, :last_name
end
Person.store = Persistent::Store.new('/path/to/store_file.store') # or anything that looks like a Hash.
o = Person.new
o.name = 'Ismael'
o.last_name = 'Celis'
# == Save and namespace unique ID by class name
o.persist! 'some_unique_id'
o2 = Person.find( 'some_unique_id' )
o2.name # => 'Ismael'
o2 == o # => true
o.destroy!
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/persistent.rb', line 32 def self.included(base) base.extend ClassMethods class << base attr_accessor :store end # The only reason an instance wants to know about their persistence id is so we can call destroy! on it. # Why not use auto-generated ID's? Because I might want to use them as URL permalinks, for example # base.class_eval do attr_accessor :_persisted_id end end |
Instance Method Details
#destroy! ⇒ Object
51 52 53 54 |
# File 'lib/persistent.rb', line 51 def destroy! return false unless self._persisted_id store.delete(persistent_key(_persisted_id)) end |
#persist!(id) ⇒ Object
46 47 48 49 |
# File 'lib/persistent.rb', line 46 def persist!(id) self._persisted_id = id store[persistent_key(id)] = self end |