Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Polaroid Preview/Example.
class Emails::OrganizerFundraisingTips
include Polaroid.new(:rsvp_deadline, :meal_page_url)
attr_reader :event
def initialize(event)
@event = event
end
def rsvp_deadline
event.rsvp_confirmation_deadline.to_s :written_date
end
def meal_page_url
event.public_url
end
end
class Polaroid < Module
def initialize(*messages)
@messages = messages
@polaroid_struct_class = ImmutableStruct.new(*messages)
define_capture_method
freeze
end
def included(base)
base.const_set(:Snapshot, @polaroid_struct_class)
base.extend(ClassMethods)
end
private #######################################################################
def define_capture_method
messages = @messages
define_method(:take_snapshot) do ||
self.class::Snapshot.new(*(messages.map { |msg| self.send(msg) })).to_h
end
end
module ClassMethods
def build_from_snapshot(snapshot_hash)
self::Snapshot.new(snapshot_hash)
end
end
end
email_viewmodel = Emails::OrganizerFundraisingTips.new(some_event)
# Capture the object' publicly-observable snapshot
snapshot = email_viewmodel.take_snapshot
# => {:rsvp_deadline=>"June 25, 2013", :meal_page_url=>"http://www.groupraise.com/events/1159"}
# Do a lot of stuff.
# Maybe somewhere far away, maybe in a background job:
viewmodel_snapshot = Emails::OrganizerFundraisingTips.build_from_snapshot(snapshot)
# => #<struct Emails::OrganizerFundraisingTips::Snapshot rsvp_deadline="June 25, 2013", meal_page_url="http://www.groupraise.com/events/1159">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.