Class: AuctionFunCore::Operations::BidContext::CreateBidStandardOperation

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

Overview

Operation class for create new bids for standard auctions.

Constant Summary collapse

INCREASE =
0.1

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(attributes, &block) ⇒ Object

TODO:

Add custom doc



16
17
18
19
20
21
22
# File 'lib/auction_fun_core/operations/bid_context/create_bid_standard_operation.rb', line 16

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

  return operation unless block

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

Instance Method Details

#calculate_standard_auction_new_minimal_bid_value(auction_id, bid_cents) ⇒ Object

Calculates the new minimum next bid amount.



50
51
52
53
54
# File 'lib/auction_fun_core/operations/bid_context/create_bid_standard_operation.rb', line 50

def calculate_standard_auction_new_minimal_bid_value(auction_id, bid_cents)
  minimal_bid_cents = (bid_cents + (bid_cents * INCREASE))

  Success(auction_repository.update(auction_id, minimal_bid_cents: minimal_bid_cents))
end

#call(attributes) ⇒ Object

TODO:

Add custom doc



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/auction_fun_core/operations/bid_context/create_bid_standard_operation.rb', line 25

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

  bid_repository.transaction do |_t|
    @bid = yield persist(values)
    yield calculate_standard_auction_new_minimal_bid_value(@bid.auction_id, @bid.value_cents)
    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.

Parameters:

  • result (Hash)

    Bid validated attributes

Returns:

  • (ROM::Struct::Bid)


59
60
61
# File 'lib/auction_fun_core/operations/bid_context/create_bid_standard_operation.rb', line 59

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

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

Triggers the publication of event bids.created.

Parameters:

  • bid (ROM::Struct::Bid)

    Bid Object

Returns:

  • (Dry::Monads::Result::Success)


66
67
68
69
70
# File 'lib/auction_fun_core/operations/bid_context/create_bid_standard_operation.rb', line 66

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.

Parameters:

  • attrs (Hash)

    bid attributes

Returns:

  • (Dry::Monads::Result::Success, Dry::Monads::Result::Failure)


41
42
43
44
45
46
47
# File 'lib/auction_fun_core/operations/bid_context/create_bid_standard_operation.rb', line 41

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

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

  Success(contract.to_h)
end