Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Polaroid Preview/Example.

View organizer_fundraising_tips.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
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
View organizer_fundraising_tips.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
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
View organizer_fundraising_tips.rb
1 2 3 4 5 6 7 8 9 10 11
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.