Skip to content

Instantly share code, notes, and snippets.

@cored
Created April 23, 2018 19:14
Show Gist options
  • Save cored/4bba0db4c7b229f8fc97e6d91ae6e5d9 to your computer and use it in GitHub Desktop.
Save cored/4bba0db4c7b229f8fc97e6d91ae6e5d9 to your computer and use it in GitHub Desktop.
# 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