Skip to content

Instantly share code, notes, and snippets.

@marcosinger
Created January 3, 2019 19:32
Show Gist options
  • Save marcosinger/5ff505639fc6bb0df0e3cdb9d84a7b62 to your computer and use it in GitHub Desktop.
Save marcosinger/5ff505639fc6bb0df0e3cdb9d84a7b62 to your computer and use it in GitHub Desktop.
Classe de exemplo para determinar se um estabelecimento está aberto no momento
class Sample
HOURS = {
'mon' => [],
'tue' => [
['09:00', '19:00'],
],
'wed' => [
['09:00', '19:00'],
],
'thu' => [
['09:00', '19:00'],
],
'fri' => [
['13:00', '19:00'],
],
'sat' => [
['09:00', '15:00'],
['17:00', '20:30'],
],
'sun' => [
['09:00', '15:00'],
],
}
def self.open?(datetime)
weekday = datetime.strftime('%a').downcase
hour = datetime.strftime('%H:%M')
ranges = HOURS[weekday].map { |item| Range.new(item.first, item.last) }
ranges.any? { |item| item.cover?(hour) }
end
end
@marcosinger
Copy link
Author

marcosinger commented Jan 3, 2019

Output

irb(main):075:0> Sample.open? Time.new(2018, 12, 31, 14)
=> false
irb(main):076:0> Sample.open? Time.new(2019, 1, 5, 17, 01, 0)
=> true
irb(main):077:0> Sample.open? Time.new(2019, 1, 5, 20, 31, 0)
=> false
irb(main):078:0> Sample.open? Time.now
=> true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment