Created
May 12, 2015 00:43
-
-
Save shurikk/3c7ddd925d4e0eb70054 to your computer and use it in GitHub Desktop.
crontab string eval
This file contains 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 Foo | |
def schedule | |
# crontab like string, e.g. "5 * * * *" | |
end | |
def run?(time) | |
fields = schedule.split(/\s+/) | |
!%w(min hour day month wday).map.with_index do |name, i| | |
fields[i].gsub(/(?:(?![\d\/\*,]).)*/, '').split(',').select do |v| | |
now = time.send(name.to_sym).to_s | |
v.gsub(/\*/, now) == now | |
end.size > 0 | |
end.include?(false) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment