Class: AuctionFunCore::Operations::StaffContext::RegistrationOperation
- Defined in:
- lib/auction_fun_core/operations/staff_context/registration_operation.rb
Overview
Operation class for create new staff member.
Class Method Summary collapse
Instance Method Summary collapse
-
#assign_default_values(attrs) ⇒ Dry::Monads::Result::Success
By default, there can only be a single root staff.
- #call(attributes) ⇒ Object
-
#encrypt_password(attrs) ⇒ Hash
Transforms the password attribute, encrypting it to be saved in the database.
-
#persist(result) ⇒ ROM::Struct::Staff
Calls the staff repository class to persist the attributes in the database.
-
#publish_staff_registration(staff_id) ⇒ Dry::Monads::Result::Success
Triggers the publication of event staffs.registration.
-
#validate_contract(attrs) ⇒ Dry::Monads::Result::Success, Dry::Monads::Result::Failure
Calls registration contract class to perform the validation of the informed attributes.
Class Method Details
.call(attributes, &block) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/auction_fun_core/operations/staff_context/registration_operation.rb', line 13 def self.call(attributes, &block) operation = new.call(attributes) return operation unless block Dry::Matcher::ResultMatcher.call(operation, &block) end |
Instance Method Details
#assign_default_values(attrs) ⇒ Dry::Monads::Result::Success
By default, there can only be a single root staff. All other members have their type set to ‘common’.
51 52 53 54 55 |
# File 'lib/auction_fun_core/operations/staff_context/registration_operation.rb', line 51 def assign_default_values(attrs) attrs[:kind] = "common" Success(attrs) end |
#call(attributes) ⇒ Object
Add custom doc
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/auction_fun_core/operations/staff_context/registration_operation.rb', line 22 def call(attributes) values = yield validate_contract(attributes) values = yield assign_default_values(values) values_with_encrypt_password = yield encrypt_password(values) staff_repository.transaction do |_t| @staff = yield persist(values_with_encrypt_password) yield publish_staff_registration(@staff.id) end Success(@staff) end |
#encrypt_password(attrs) ⇒ Hash
Transforms the password attribute, encrypting it to be saved in the database.
60 61 62 63 64 65 66 |
# File 'lib/auction_fun_core/operations/staff_context/registration_operation.rb', line 60 def encrypt_password(attrs) attributes = attrs.to_h.except(:password) Success( {**attributes, password_digest: BCrypt::Password.create(attrs[:password])} ) end |
#persist(result) ⇒ ROM::Struct::Staff
Calls the staff repository class to persist the attributes in the database.
71 72 73 |
# File 'lib/auction_fun_core/operations/staff_context/registration_operation.rb', line 71 def persist(result) Success(staff_repository.create(result)) end |
#publish_staff_registration(staff_id) ⇒ Dry::Monads::Result::Success
Triggers the publication of event staffs.registration.
78 79 80 81 82 |
# File 'lib/auction_fun_core/operations/staff_context/registration_operation.rb', line 78 def publish_staff_registration(staff_id) staff = staff_repository.by_id!(staff_id) Success(Application[:event].publish("staffs.registration", staff.info)) end |
#validate_contract(attrs) ⇒ Dry::Monads::Result::Success, Dry::Monads::Result::Failure
Calls registration contract class to perform the validation of the informed attributes.
40 41 42 43 44 45 46 |
# File 'lib/auction_fun_core/operations/staff_context/registration_operation.rb', line 40 def validate_contract(attrs) contract = registration_contract.call(attrs) return Failure(contract.errors.to_h) if contract.failure? Success(contract.to_h) end |