Last active
September 20, 2020 20:46
-
-
Save serradura/af28e676ffeb834df0e2f65c986232bd to your computer and use it in GitHub Desktop.
haroldo - u-case
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
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'awesome_print' | |
gem 'pry' | |
gem 'u-case', '~> 4.0.0' | |
gem 'activemodel', '~> 6.0' | |
end | |
class Sum < Micro::Case | |
attributes :a, :b | |
def call! | |
if Kind.of?(Numeric, a, b) | |
Success result: { number: a + b } | |
else | |
Failure(:attributes_must_be_numbers) | |
end | |
end | |
end | |
binding.pry |
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
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'awesome_print' | |
gem 'pry' | |
gem 'u-case', '~> 4.0.0' | |
end | |
class Add3 < Micro::Case | |
attribute :number | |
def call! | |
if Kind.of?(Numeric, number) | |
Success result: { number: number + 3 } | |
else | |
Failure(:attribute_must_be_a_number) | |
end | |
end | |
end | |
Add6 = Micro::Cases.flow([Add3, Add3]) | |
# r1 = Add6.call(number: 1) | |
class Double < Micro::Case | |
attribute :number | |
def call! | |
if Kind.of?(Numeric, number) | |
Success result: { number: number * 2 } | |
else | |
Failure(:attribute_must_be_a_number) | |
end | |
end | |
end | |
# r2 = Add6.call(number: 1).then(Double) | |
Add6AndDouble = Micro::Cases.flow([Add6, Double]) | |
# r3 = Add6AndDouble.call(number: 1) | |
binding.pry |
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
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'awesome_print' | |
gem 'pry' | |
gem 'u-case', '~> 4.0.0' | |
gem 'activemodel', '~> 6.0' | |
end | |
Micro::Case.config do |config| | |
config.enable_activemodel_validation = true | |
end | |
class CreateUser1 < Micro::Case | |
attributes :name, :email, :password | |
def call! | |
if Kind.of?(String, name, email, password) | |
Success(:user_created) if [name, email, password].none?(&:empty?) | |
else | |
Failure(:attributes_are_invalid) | |
end | |
end | |
end | |
# r = CreateUser1.call(name: 'Test', email: '[email protected]', password: '123456') | |
class CreateUser2 < Micro::Case | |
attributes :name, :email, :password | |
validates :name, :email, :password, kind: String, presence: true, allow_nil: true | |
def call! | |
Success(:user_created) | |
end | |
end | |
# r = CreateUser2.call(name: nil, email: '[email protected]', password: '123456') | |
class CreateUser3 < Micro::Case | |
attributes :name, :email, :password, validates: { kind: String, presence: true } | |
def call! | |
Success(:user_created) | |
end | |
end | |
# r = CreateUser3.call(name: nil, email: '[email protected]', password: '123456') | |
binding.pry |
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
# https://t.me/rubybrasil/148098 | |
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'awesome_print' | |
gem 'pry' | |
gem 'u-case', '~> 4.0.0' | |
gem 'activemodel', '~> 6.0' | |
end | |
Micro::Case.config do |config| | |
config.enable_activemodel_validation = true | |
end | |
require 'securerandom' | |
# 1. Recebe email e senha e normaliza | |
# - Tira espaços das strings | |
# 2. Valida o email e nome | |
# - Nome tem que ser uma string preenchida | |
# - Email tem que passar por uma regex | |
# 3. Salvar o usuario | |
# 4. Enviar para o CRM | |
class User | |
include Micro::Attributes.with(:initialize) | |
attributes :id, :name, :email, required: true | |
end | |
class CreateUserAndSyncWithCrm1 < Micro::Case | |
attributes :name, :email | |
def call! | |
normalized_name = name.to_s.strip | |
normalized_email = email.to_s.strip | |
validations_error = [] | |
validations_error << "name can't be blank" if name.empty? | |
validations_error << "email is invalid" if email !~ /.+@.+\..+/ | |
unless validations_error.empty? | |
return Failure(:invalid_attributes, result: { errors: validations_error }) | |
end | |
user = User.new( | |
id: SecureRandom.uuid, | |
name: normalized_name, | |
email: normalized_email | |
) | |
crm_id = sync_with_crm(user) | |
Success result: {user: user, crm_id: crm_id } | |
end | |
private | |
def sync_with_crm(user) | |
return SecureRandom.uuid if user.id | |
end | |
end | |
r1 = CreateUserAndSyncWithCrm1.call(name: '', email: nil) | |
r2 = CreateUserAndSyncWithCrm1.call(name: '', email: '[email protected]') | |
r3 = CreateUserAndSyncWithCrm1.call(name: 'Test ', email: '[email protected] ') | |
# --- | |
class CreateUserAndSyncWithCrm2 < Micro::Case | |
attributes :name, :email | |
def call! | |
normalize_attributes | |
.then(method(:validate_attributes)) | |
.then(method(:create_user)) | |
.then(method(:sync_with_crm)) | |
end | |
private | |
def normalize_attributes | |
Success(:normalize_attributes, result: { | |
name: name.to_s.strip, | |
email: email.to_s.strip | |
}) | |
end | |
def validate_attributes(email:, name:) | |
validations_error = [] | |
validations_error << "name can't be blank" if name.empty? | |
validations_error << "email is invalid" if email !~ /.+@.+\..+/ | |
return Success(:validate_attributes) if validations_error.empty? | |
Failure(:invalid_attributes, result: { errors: validations_error }) | |
end | |
def create_user(name:, email:, **) | |
user = User.new( | |
id: SecureRandom.uuid, | |
name: name, | |
email: email | |
) | |
Success(:create_user, result: { user: user }) | |
end | |
def sync_with_crm(user:, **) | |
return Failure(:cant_sync) unless user.id | |
Success(:sync_with_crm, result: { | |
crm_id: SecureRandom.uuid, | |
user: user | |
}) | |
end | |
end | |
r4 = CreateUserAndSyncWithCrm2.call(name: '', email: nil) | |
r5 = CreateUserAndSyncWithCrm2.call(name: '', email: '[email protected]') | |
r6 = CreateUserAndSyncWithCrm2.call(name: ' Test ', email: ' [email protected]') | |
# -- | |
class CreateUser1 < Micro::Case | |
attributes :name, :email | |
def call! | |
user = User.new( | |
id: SecureRandom.uuid, | |
name: name, | |
email: email | |
) | |
Success(result: { user: user }) | |
end | |
end | |
class CreateUserAndSyncWithCrm3 < Micro::Case | |
attributes :name, :email | |
def call! | |
normalize_attributes | |
.then(method(:validate_attributes)) | |
.then(CreateUser1) | |
.then(method(:sync_with_crm)) | |
end | |
private | |
def normalize_attributes | |
Success(:normalize_attributes, result: { | |
name: name.to_s.strip, | |
email: email.to_s.strip | |
}) | |
end | |
def validate_attributes(email:, name:) | |
validations_error = [] | |
validations_error << "name can't be blank" if name.empty? | |
validations_error << "email is invalid" if email !~ /.+@.+\..+/ | |
return Success(:validate_attributes) if validations_error.empty? | |
Failure(:invalid_attributes, result: { errors: validations_error }) | |
end | |
def sync_with_crm(user:, **) | |
return Failure(:cant_sync) unless user.id | |
Success(:sync_with_crm, result: { | |
crm_id: SecureRandom.uuid, | |
user: user | |
}) | |
end | |
end | |
r7 = CreateUserAndSyncWithCrm3.call(name: '', email: nil) | |
r8 = CreateUserAndSyncWithCrm3.call(name: '', email: '[email protected]') | |
r9 = CreateUserAndSyncWithCrm3.call(name: ' Test ', email: ' [email protected]') | |
# --- | |
class NormalizeAttributes < Micro::Case | |
attributes :name, :email | |
def call! | |
Success(result: { | |
name: name.to_s.strip, | |
email: email.to_s.strip | |
}) | |
end | |
end | |
class ValidateAttributes < Micro::Case | |
attributes :name, :email | |
def call! | |
validations_error = [] | |
validations_error << "name can't be blank" if name.empty? | |
validations_error << "email is invalid" if email !~ /.+@.+\..+/ | |
return Success(:validate_attributes) if validations_error.empty? | |
Failure(:invalid_attributes, result: { errors: validations_error }) | |
end | |
end | |
class CreateUser2 < Micro::Case | |
attributes :name, :email | |
def call! | |
user = User.new( | |
id: SecureRandom.uuid, | |
name: name, | |
email: email | |
) | |
Success(result: { user: user }) | |
end | |
end | |
class SyncWithCrm < Micro::Case | |
attribute :user | |
def call! | |
return Failure(:cant_sync) unless user.id | |
Success(result: { | |
crm_id: SecureRandom.uuid, | |
user: user | |
}) | |
end | |
end | |
CreateUserAndSyncWithCrm4 = Micro::Cases.flow([ | |
NormalizeAttributes, | |
ValidateAttributes, | |
CreateUser2, | |
SyncWithCrm | |
]) | |
r10 = CreateUserAndSyncWithCrm4.call(name: '', email: nil) | |
r11 = CreateUserAndSyncWithCrm4.call(name: '', email: '[email protected]') | |
r12 = CreateUserAndSyncWithCrm4.call(name: ' Test ', email: ' [email protected]') | |
binding.pry |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment