Module: Ricordami::IsPersisted::InstanceMethods
- Defined in:
- lib/ricordami/is_persisted.rb
Instance Method Summary collapse
- #delete ⇒ Object
- #deleted? ⇒ Boolean
- #execute_delete(db_commands) ⇒ Object
- #initialize(*args) ⇒ Object
- #mark_as_deleted ⇒ Object
- #new_record? ⇒ Boolean
- #persisted? ⇒ Boolean
- #prepare_delete(session) ⇒ Object
- #redis ⇒ Object
- #reload ⇒ Object
- #save(opts = {}) ⇒ Object
- #to_s ⇒ Object
- #update_attributes(attrs) ⇒ Object
Instance Method Details
#delete ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/ricordami/is_persisted.rb', line 85 def delete raise ModelHasBeenDeleted.new("can't delete a model already deleted") if deleted? db_commands, models = [], [] # TODO: use watch (Redis 2.2) and re-run prepare + execute if change session = Struct.new(:models, :commands).new(models, db_commands) prepare_delete(session) execute_delete(db_commands) models.each { |model| model.mark_as_deleted } end |
#deleted? ⇒ Boolean
51 52 53 |
# File 'lib/ricordami/is_persisted.rb', line 51 def deleted? @deleted end |
#execute_delete(db_commands) ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/ricordami/is_persisted.rb', line 101 def execute_delete(db_commands) redis.tap do |redis| redis.multi db_commands.each do |, args| redis.send(, *args) end redis.exec end end |
#initialize(*args) ⇒ Object
37 38 39 40 41 |
# File 'lib/ricordami/is_persisted.rb', line 37 def initialize(*args) super(*args) @persisted = false unless instance_variable_defined?(:@persisted) @deleted = false end |
#mark_as_deleted ⇒ Object
111 112 113 114 115 116 |
# File 'lib/ricordami/is_persisted.rb', line 111 def mark_as_deleted attributes_synced_with_db! @deleted = true freeze true end |
#new_record? ⇒ Boolean
47 48 49 |
# File 'lib/ricordami/is_persisted.rb', line 47 def new_record? !@persisted end |
#persisted? ⇒ Boolean
43 44 45 |
# File 'lib/ricordami/is_persisted.rb', line 43 def persisted? @persisted end |
#prepare_delete(session) ⇒ Object
95 96 97 98 99 |
# File 'lib/ricordami/is_persisted.rb', line 95 def prepare_delete(session) session.models << self session.commands << [:del, [attributes_key_name]] self.class.delete_queue.reverse.each { |block| block.call(self, session) } if self.class.delete_queue end |
#redis ⇒ Object
118 119 120 |
# File 'lib/ricordami/is_persisted.rb', line 118 def redis self.class.redis end |
#reload ⇒ Object
72 73 74 75 76 77 |
# File 'lib/ricordami/is_persisted.rb', line 72 def reload attrs = self.class.send(:load_attributes_for, id) update_mem_attributes(attrs) unless attrs.empty? attributes_synced_with_db! self end |
#save(opts = {}) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ricordami/is_persisted.rb', line 55 def save(opts = {}) raise ModelHasBeenDeleted.new("can't save a deleted model") if deleted? set_initial_attribute_values if new_record? redis.tap do |redis| session = {} redis.multi redis.hmset(attributes_key_name, *attributes.to_a.flatten) self.class.save_queue.each { |block| block.call(self, session) } if self.class.save_queue redis.exec end @persisted = true attributes_synced_with_db! rescue Exception => ex raise ex if ex.is_a?(ModelHasBeenDeleted) false end |
#to_s ⇒ Object
122 123 124 125 126 127 128 129 130 |
# File 'lib/ricordami/is_persisted.rb', line 122 def to_s [ "#<#{self.class}:0x#{object_id.to_s(16)}", "persisted?(#{persisted?.inspect})", "new_record?(#{new_record?.inspect})", "deleted?(#{deleted?.inspect})", "attributes=#{attributes.inspect}>" ].join(" ") end |
#update_attributes(attrs) ⇒ Object
79 80 81 82 83 |
# File 'lib/ricordami/is_persisted.rb', line 79 def update_attributes(attrs) raise ModelHasBeenDeleted.new("can't update the attributes of a deleted model") if deleted? update_mem_attributes!(attrs) unless attrs.empty? save end |