class Mushy::Interval

Public Class Methods

details() click to toggle source
# File lib/mushy/fluxs/interval.rb, line 17
def self.details
  {
    name: 'Interval',
    description: 'Fire an event every X minutes.',
    config: {},
  }.tap do |c|
    setup.keys.each do |key|
      c[:config][key] = {
                          description: "#{key.to_s.capitalize} until the job is fired again.",
                          type:        'integer',
                          shrink:       true,
                          value:       '',
                        }
    end
  end
end
setup() click to toggle source
# File lib/mushy/fluxs/interval.rb, line 7
def self.setup
  {
    seconds: ->(x) { x },
    minutes: ->(x) { x * 60 },
    hours:   ->(x) { x * 60 * 60 },
    days:    ->(x) { x * 60 * 60 * 24 },
    weeks:   ->(x) { x * 60 * 60 * 24 * 7 },
  }
end

Public Instance Methods

loop(&block) click to toggle source
# File lib/mushy/fluxs/interval.rb, line 34
def loop &block
  event = { time: time }
  block.call event
  sleep time
end
process(event, config) click to toggle source
# File lib/mushy/fluxs/interval.rb, line 49
def process event, config
  now = Time.now
  Mushy::DateParts.parse now
end
time() click to toggle source
# File lib/mushy/fluxs/interval.rb, line 40
def time
  the_time = self.class.setup.keys
                  .select { |x| config[x].to_s != '' }
                  .map    { |x| self.class.setup[x].call(config[x].to_i) }
                  .sum

  the_time > 0 ? the_time : 60
end