Skip to content

Instantly share code, notes, and snippets.

@ahawkins
Created April 13, 2014 13:12
Show Gist options
  • Select an option

  • Save ahawkins/10583641 to your computer and use it in GitHub Desktop.

Select an option

Save ahawkins/10583641 to your computer and use it in GitHub Desktop.
class User
include Chassis::Persistance
include Serialization
include Chassis::HashInitializer
attr_accessor :name, :phone_number, :token, :device
def push?
!!device.push_token
end
def save
raise "Users must have tokens!" unless token
super
end
end
class UserRepo
# Delegate operations to the default Repo with the correct
# object_class
extend Chassis::Repo::Delegation
class UnknownTokenError < StandardError
def initialize(token)
@token = token
end
def to_s
"Could not identifiy user with token: #{@token}"
end
end
class UnknownPhoneNumber < StandardError
def initialize(phone_number)
@phone_number = phone_number
end
def to_s
"Could not identifiy user with phone number: #{@phone_number}"
end
end
# Define application specific methods used to access the objects
# in a meaningful way
class << self
def find_by_token!(token)
user = query UserWithToken.new(token)
raise UnknownTokenError, token if user.nil?
user
end
def find_by_phone_number!(phone_number)
user = query UserWithPhoneNumber.new(phone_number)
raise UnknownPhoneNumber, phone_number if user.nil?
user
end
end
end
# Define selectors which are passed when querying the repo
UserWithToken = Struct.new :token
UserWithPhoneNumber = Struct.new :phone_number
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment