mirror of
https://github.com/plashchynski/crono.git
synced 2026-05-26 01:39:17 +02:00
Rename to Crono
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
require 'crono'
|
||||
|
||||
module Crono
|
||||
class CLI
|
||||
include Singleton
|
||||
|
||||
def run
|
||||
load_rails
|
||||
print_banner
|
||||
end
|
||||
|
||||
def print_banner
|
||||
puts "Loading Crono #{Crono::VERSION}"
|
||||
puts "Running in #{RUBY_DESCRIPTION}"
|
||||
end
|
||||
|
||||
def load_rails
|
||||
require 'rails'
|
||||
require File.expand_path("config/environment.rb")
|
||||
::Rails.application.eager_load!
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,11 @@
|
||||
module Crono
|
||||
class Config
|
||||
include Singleton
|
||||
|
||||
attr_accessor :schedule
|
||||
|
||||
def initialize
|
||||
self.schedule = []
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,15 @@
|
||||
module Crono
|
||||
class PerformerProxy
|
||||
def initialize(performer)
|
||||
@performer = performer
|
||||
end
|
||||
|
||||
def every(period, *args)
|
||||
Config.instance.schedule += [@performer, Period.new(period, *args)]
|
||||
end
|
||||
end
|
||||
|
||||
def self.perform(performer)
|
||||
PerformerProxy.new(performer)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,24 @@
|
||||
module Crono
|
||||
class Period
|
||||
def initialize(period, at: nil)
|
||||
@period = period
|
||||
@at_hour, @at_min = parse_at(at) if at
|
||||
end
|
||||
|
||||
def next
|
||||
@period.from_now.change({hour: @at_hour, min: @at_min}.compact)
|
||||
end
|
||||
|
||||
def parse_at(at)
|
||||
case at
|
||||
when String
|
||||
time = Time.parse(at)
|
||||
return time.hour, time.min
|
||||
when Hash
|
||||
return at[:hour], at[:min]
|
||||
else
|
||||
raise "Unknown 'at' format"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,3 @@
|
||||
module Crono
|
||||
VERSION = "0.0.1"
|
||||
end
|
||||
Reference in New Issue
Block a user