Skip to content

Instantly share code, notes, and snippets.

@azisaka
Created March 13, 2013 15:12
Show Gist options
  • Save azisaka/5153084 to your computer and use it in GitHub Desktop.
Save azisaka/5153084 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
# ...
def self.sync_users
users_to_sync = where synced: false
SyncUsers.new(users_to_sync).tap &:sync
end
# ...
end
class UsersSyncer
attr_reader :users_to_sync
def initialize(users_to_sync)
@users_to_sync = users_to_sync
end
def sync
users_to_sync.each(&:sync!)
@has_synced_users = true
end
def synced_users
has_synced_users? ? users_to_sync : []
end
def has_synced_users?
!!@has_synced_users
end
end
sync_users = User.sync_users
synced_users = sync_users.synced_users
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment