Skip to content

Instantly share code, notes, and snippets.

@Aupajo
Last active October 7, 2015 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aupajo/d8600818581bde619904 to your computer and use it in GitHub Desktop.
Save Aupajo/d8600818581bde619904 to your computer and use it in GitHub Desktop.
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