Created
April 23, 2018 19:14
-
-
Save cored/4bba0db4c7b229f8fc97e6d91ae6e5d9 to your computer and use it in GitHub Desktop.
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
# frozen_string_literal: true | |
module WarehouseAssignment | |
module Rates | |
class UpdateWithCheapest | |
def self.call(options:) | |
new(options: Array(options)).to_a | |
end | |
def initialize(options:) | |
@options = options | |
end | |
def to_a | |
mark_option_as_cheapest! | |
end | |
private | |
def cheapest_rate | |
@cheapest_rate ||= options.reject do |option| | |
option.rate.is_a?(String) | |
end.min_by(&:rate).rate | |
end | |
def mark_option_as_cheapest! | |
options.each do |option| | |
option.cheapest = true if option.rate == cheapest_rate | |
end | |
end | |
attr_reader :options | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment