Module: Toy::Persistence
- Extended by:
- ActiveSupport::Concern
- Included in:
- DirtyStore, Store
- Defined in:
- lib/toy/persistence.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #adapter ⇒ Object
- #delete ⇒ Object
- #destroy ⇒ Object
- #destroyed? ⇒ Boolean
- #initialize(attrs = {}) ⇒ Object
- #initialize_copy(other) ⇒ Object
- #initialize_from_database(attrs = {}) ⇒ Object
- #new_record? ⇒ Boolean
-
#persist ⇒ Object
Public: Choke point for overriding how data gets written.
- #persisted? ⇒ Boolean
-
#persisted_attributes ⇒ Object
Public: Choke point for overriding what attributes get stored.
-
#persisted_id ⇒ Object
Public: Choke point for overriding what id is used to write and delete.
- #save(options = {}) ⇒ Object
- #update_attributes(attrs) ⇒ Object
Instance Method Details
#adapter ⇒ Object
44 45 46 |
# File 'lib/toy/persistence.rb', line 44 def adapter self.class.adapter end |
#delete ⇒ Object
108 109 110 111 |
# File 'lib/toy/persistence.rb', line 108 def delete @_destroyed = true adapter.delete(persisted_id) end |
#destroy ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/toy/persistence.rb', line 97 def destroy default_payload = { :id => persisted_id, :model => self.class, } Toy.instrumenter.instrument('destroy.toystore', default_payload) { |payload| delete } end |
#destroyed? ⇒ Boolean
70 71 72 |
# File 'lib/toy/persistence.rb', line 70 def destroyed? @_destroyed == true end |
#initialize(attrs = {}) ⇒ Object
48 49 50 51 |
# File 'lib/toy/persistence.rb', line 48 def initialize(attrs={}) @_new_record = true super end |
#initialize_copy(other) ⇒ Object
60 61 62 63 64 |
# File 'lib/toy/persistence.rb', line 60 def initialize_copy(other) super @_new_record = true @_destroyed = false end |
#initialize_from_database(attrs = {}) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/toy/persistence.rb', line 53 def initialize_from_database(attrs={}) @_new_record = false initialize_attributes send("attributes=", attrs, false) self end |
#new_record? ⇒ Boolean
66 67 68 |
# File 'lib/toy/persistence.rb', line 66 def new_record? @_new_record == true end |
#persist ⇒ Object
Public: Choke point for overriding how data gets written. Don’t call this directory, but you can safely override it.
134 135 136 |
# File 'lib/toy/persistence.rb', line 134 def persist adapter.write(persisted_id, persisted_attributes) end |
#persisted? ⇒ Boolean
74 75 76 |
# File 'lib/toy/persistence.rb', line 74 def persisted? !new_record? && !destroyed? end |
#persisted_attributes ⇒ Object
Public: Choke point for overriding what attributes get stored.
122 123 124 125 126 127 128 129 130 |
# File 'lib/toy/persistence.rb', line 122 def persisted_attributes attributes = {} self.class.persisted_attributes.each do |attribute| if (value = attribute.to_store(read_attribute(attribute.name))) attributes[attribute.persisted_name] = value end end attributes end |
#persisted_id ⇒ Object
Public: Choke point for overriding what id is used to write and delete.
114 115 116 117 118 119 |
# File 'lib/toy/persistence.rb', line 114 def persisted_id attribute_name = 'id' attribute = attribute_instance(attribute_name) attribute_value = read_attribute(attribute_name) attribute.to_store(attribute_value) end |
#save(options = {}) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/toy/persistence.rb', line 78 def save(={}) default_payload = { :id => persisted_id, :model => self.class, } new_record = new_record? action = new_record ? 'create' : 'update' Toy.instrumenter.instrument("#{action}.toystore", default_payload) { |payload| new_record ? create : update } end |
#update_attributes(attrs) ⇒ Object
92 93 94 95 |
# File 'lib/toy/persistence.rb', line 92 def update_attributes(attrs) self.attributes = attrs save end |