Skip to content

Instantly share code, notes, and snippets.

@sunny
Last active April 2, 2020 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunny/bee46ce56dcfc4481caebf5817ed2134 to your computer and use it in GitHub Desktop.
Save sunny/bee46ce56dcfc4481caebf5817ed2134 to your computer and use it in GitHub Desktop.
class ComputePrice < Actor
play ComputePricePerDay,
if: -> input { input.selected_at < Time.utc(2020, 9, 1) }
play ComputePricePerMinute,
if: -> input { Time.utc(2020, 9, 1) <= input.selected_at }
play ApplyDiscount,
if: -> input { Time.utc(2020, 6, 1) <= input.selected_at }
end
class ComputePricePerDay < Actor
input :period
input :car_price_per_day
output :price
def call
self.price = period.duration_in_days * car_price_per_day
end
end
class ComputePricePerMinute < Actor
input :period
input :car_price_per_day
output :price
def call
price_per_minute = car_price_per_day / (24 * 60).to_f
self.price = period.duration_in_minutes * price_per_minute
end
end
class ApplyDiscount < Actor
input :period
input :price
output :price
def call
discount = (2 * period.duration_in_days - 4).clamp(0, 40)
self.price *= 1 - discount / 100.0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment