Skip to content

Instantly share code, notes, and snippets.

@ahawkins
Last active August 29, 2015 13:59
Show Gist options
  • Save ahawkins/10583642 to your computer and use it in GitHub Desktop.
Save ahawkins/10583642 to your computer and use it in GitHub Desktop.
class User
include Chassis::Persistence
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
@inem
Copy link

inem commented Apr 18, 2014

This should have been happen. And it happen.
In chassis you have PersistEnce, but here you typed PersistAnce
Also, there's no such thing like HashInitializer in Chassis. Did you meant Initializable?

And where did Serialization came from? ActiveModel::Serialization?

@ahawkins
Copy link
Author

@inem sorry about! The code was taken from a project using an older version of chassis. Chassis::HashInitializer was in an older version. It's been replaced with Chassis::Initializable which uses a block and hash. Chassis::Persistence now includes Chassis::Initializable. Serialization was a custom module I wrote for that project. It should not have been in the example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment