Class: Mushy::Interval

Inherits:
Flux
  • Object
show all
Defined in:
lib/mushy/fluxs/interval.rb

Instance Attribute Summary

Attributes inherited from Flux

#config, #flow, #id, #masher, #parent_fluxs, #subscribed_to, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Flux

#convert_this_to_an_array, #convert_to_symbolized_hash, #execute, #execute_single_event, #group_these_results, #guard, #ignore_these_results, inherited, #initialize, #join_these_results, #limit_these_results, #merge_these_results, #model_these_results, #outgoing_split_these_results, #shape_these, #sort_these_results, #standardize_these

Constructor Details

This class inherits a constructor from Mushy::Flux

Class Method Details

.detailsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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

.setupObject



7
8
9
10
11
12
13
14
15
# 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

Instance Method Details

#loop(&block) ⇒ Object



34
35
36
37
38
# File 'lib/mushy/fluxs/interval.rb', line 34

def loop &block
  event = { time: time }
  block.call event
  sleep time
end

#process(event, config) ⇒ Object



49
50
51
52
# File 'lib/mushy/fluxs/interval.rb', line 49

def process event, config
  now = Time.now
  Mushy::DateParts.parse now
end

#timeObject



40
41
42
43
44
45
46
47
# 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