Last active
August 29, 2015 13:59
-
-
Save ahawkins/10583643 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MemoryRepo < Chassis:MemoryRepo | |
def find(klass, id) | |
super klass, id.to_i | |
end | |
def query_auth_token_with_code(klass, q) | |
all(klass).find do |auth_token| | |
auth_token.code == q.code | |
end | |
end | |
# Implement a method to handle the specific selector | |
# It's simple enough to just load all records then make | |
# find/select calls to get the right stuff. | |
def query_user_with_token(klass, q) | |
all(klass).find do |user| | |
user.token == q.token | |
end | |
end | |
def query_user_with_phone_number(klass, q) | |
all(klass).find do |user| | |
user.phone_number == q.phone_number | |
end | |
end | |
def query_groups_for_user(klass, q) | |
set = all(klass).select do |group| | |
group.users.include? q.user | |
end | |
if q.updated_after | |
set.select! do |group| | |
group.updated_at.utc >= q.updated_after.utc | |
end | |
end | |
set | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User | |
# Connect this class to the UserRepo | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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