lib/toy/persistence.rb in toystore-0.13.0 vs lib/toy/persistence.rb in toystore-0.13.1

- old
+ new

@@ -28,10 +28,19 @@ end def destroy(*ids) ids.each { |id| get(id).try(:destroy) } end + + def persisted_attributes + @persisted_attributes ||= attributes.values.select(&:persisted?) + end + + def attribute(*args) + @persisted_attributes = nil + super + end end def adapter self.class.adapter end @@ -79,13 +88,37 @@ delete end def delete @_destroyed = true - adapter.delete(id) + adapter.delete(persisted_id) end + # Public: Choke point for overriding what id is used to write and delete. + def persisted_id + attribute_name = 'id' + attribute = attribute_instance(attribute_name) + attribute_value = read_attribute(attribute_name) + attribute.to_store(attribute_value) + end + + # Public: Choke point for overriding what attributes get stored. + 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 + + # Public: Choke point for overriding how data gets written. + def persist + adapter.write(persisted_id, persisted_attributes) + end + private def create persist @_new_record = false @@ -93,12 +126,8 @@ end def update persist true - end - - def persist - adapter.write(id, persisted_attributes) end end end