Class: SidekiqSimpleDelay::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq_simple_delay/utils.rb

Overview

utility methods

Constant Summary collapse

SYSTEM_SIMPLE_CLASSES =
Set.new(
  [
    NilClass,
    TrueClass,
    FalseClass,
    String,
    Symbol
  ]
).freeze
SYSTEM_SIMPLE_COMPLEX_CLASSES =
Set.new(
  [
    Hash,
    Array
  ]
).freeze
SYSTEM_SIMPLE_NUMERIC_CLASSES =
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.4.0')
  Set.new([Integer, Float]).freeze
else
  Set.new([Fixnum, Bignum, Float]).freeze
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extract_option(opts, arg, default = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/sidekiq_simple_delay/utils.rb', line 49

def extract_option(opts, arg, default = nil)
  [arg.to_sym, arg.to_s].each do |a|
    next unless opts.key?(a)

    return opts.delete(a)
  end

  default
end

.simple_object?(obj) ⇒ Boolean



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sidekiq_simple_delay/utils.rb', line 34

def simple_object?(obj)
  klass = obj.class

  if SYSTEM_SIMPLE_COMPLEX_CLASSES.include?(klass)
    obj.all? { |o| simple_object?(o) }
  elsif SYSTEM_SIMPLE_CLASSES.include?(klass)
    true
  elsif SYSTEM_SIMPLE_NUMERIC_CLASSES.include?(klass)
    true
  else
    false
  end
end

Instance Method Details

#random_number(duration) ⇒ Object



60
61
62
# File 'lib/sidekiq_simple_delay/utils.rb', line 60

def random_number(duration)
  SecureRandom.random_number(duration)
end