Class: AuctionFunCore::Operations::BidContext::CreateBidPennyOperation

Inherits:
AuctionFunCore::Operations::Base show all
Defined in:
lib/auction_fun_core/operations/bid_context/create_bid_penny_operation.rb

Overview

Operation class for create new bids for penny auctions.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(attributes, &block) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/auction_fun_core/operations/bid_context/create_bid_penny_operation.rb', line 14

def self.call(attributes, &block)
  operation = new.call(attributes)

  return operation unless block

  Dry::Matcher::ResultMatcher.call(operation, &block)
end

Instance Method Details

#call(attributes) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/auction_fun_core/operations/bid_context/create_bid_penny_operation.rb', line 23

def call(attributes)
  values = yield validate(attributes)

  bid_repository.transaction do |_t|
    @bid = yield persist(values)
    yield publish_bid_created(@bid)
  end

  Success(@bid)
end

#persist(result) ⇒ ROM::Struct::Bid

Calls the bid repository class to persist the attributes in the database.



49
50
51
# File 'lib/auction_fun_core/operations/bid_context/create_bid_penny_operation.rb', line 49

def persist(result)
  Success(bid_repository.create(result))
end

#publish_bid_created(bid) ⇒ Dry::Monads::Result::Success

Triggers the publication of event bids.created.



56
57
58
59
60
# File 'lib/auction_fun_core/operations/bid_context/create_bid_penny_operation.rb', line 56

def publish_bid_created(bid)
  Application[:event].publish("bids.created", bid.to_h)

  Success()
end

#validate(attrs) ⇒ Dry::Monads::Result::Success, Dry::Monads::Result::Failure

Calls the bid creation contract class to perform the validation of the informed attributes.



38
39
40
41
42
43
44
# File 'lib/auction_fun_core/operations/bid_context/create_bid_penny_operation.rb', line 38

def validate(attrs)
  contract = create_bid_penny_contract.call(attrs)

  return Failure(contract.errors.to_h) if contract.failure?

  Success(contract.to_h)
end