Created
October 7, 2012 15:55
-
-
Save petelacey/3848733 to your computer and use it in GitHub Desktop.
An Actual Use Case
This file contains 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
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