Skip to content

Instantly share code, notes, and snippets.

@petelacey
Created October 7, 2012 15:55
Show Gist options
  • Save petelacey/3848733 to your computer and use it in GitHub Desktop.
Save petelacey/3848733 to your computer and use it in GitHub Desktop.
An Actual Use Case
require 'sonian/use_cases/password_encryption'
require 'sonian/values/user'
require 'active_support/core_ext/object/blank'
module Sonian
class CreateUser
def initialize(account_vo, data_store)
raise ArgumentError.new unless account_vo && data_store
@account = account_vo
@user_gateway = data_store.user_gateway
end
def with_attributes(attributes)
attrs = attributes.dup
validate_attributes(attrs)
attrs[:password] = PasswordEncryption.new.encrypt(attrs[:password])
User.new(attrs).tap do |user|
@user_gateway.add_user(@account, user)
end
end
private
def validate_attributes(attributes)
username = attributes[:username]
password = attributes[:password]
raise ArgumentError.new("Username and password must not be blank") if username.blank? || password.blank?
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment