Last active
October 7, 2015 01:24
-
-
Save Aupajo/d8600818581bde619904 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person | |
attr_reader :attributes, :original_attributes | |
def initialize(original = {}) | |
@original_attributes = original | |
@attributes = original.clone | |
end | |
def sandwich=(value) | |
attributes[:sandwich] = value | |
end | |
def destroy | |
PersonPersistence.destroy(person) | |
end | |
def save | |
PersonPersistence.save(self) | |
end | |
def self.find(identifier) | |
self.new(PersonPersistence.find_attributes(identifier)) | |
end | |
end | |
class PersonPersistence | |
def initialize(person) | |
@person = person | |
end | |
def find_attributes(identifier) | |
attributes = {} | |
attributes.merge! GigyaPersonPeristence.find_attributes(identifier) | |
attributes.merge! DatabasePersonPeristence.find_attributes(identifier) | |
end | |
def save | |
transaction do | |
GigyaPersonPeristence.save(person) | |
DatabasePersonPeristence.save(person) | |
end | |
end | |
def changes | |
person.original_attributes.diff(person.attributes) | |
end | |
def self.save(*args) | |
self.new(*args).save | |
end | |
end | |
class GigyaPersonPeristence < PersonPersistence | |
def save | |
# calls to gigya | |
end | |
def changes | |
super.slice(:attrs, :i, :care, :about) | |
end | |
end | |
class DatabasePersonPeristence < PersonPersistence | |
def save | |
record.update(changes) | |
end | |
def changes | |
super.slice(:attrs, :i, :care, :about) | |
end | |
private | |
def record | |
PersonRecord.find(person.id) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment