Created
January 3, 2019 19:32
-
-
Save marcosinger/5ff505639fc6bb0df0e3cdb9d84a7b62 to your computer and use it in GitHub Desktop.
Classe de exemplo para determinar se um estabelecimento está aberto no momento
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output