Module: Newshound::Jobs

Defined in:
lib/newshound/jobs.rb,
lib/newshound/jobs/que.rb,
lib/newshound/jobs/base.rb

Defined Under Namespace

Classes: Base, Que

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.registryObject (readonly)

Returns the value of attribute registry.



10
11
12
# File 'lib/newshound/jobs.rb', line 10

def registry
  @registry
end

Class Method Details

.clear_registry!Object



38
39
40
# File 'lib/newshound/jobs.rb', line 38

def clear_registry!
  @registry = {}
end

.register(name, adapter_class) ⇒ Object

Register a job adapter class with a symbolic name

Parameters:

  • name (Symbol, String)

    The name to register the adapter under

  • adapter_class (Class)

    The adapter class (must inherit from Jobs::Base)



16
17
18
# File 'lib/newshound/jobs.rb', line 16

def register(name, adapter_class)
  @registry[name.to_sym] = adapter_class
end

.source(source) ⇒ Jobs::Base

Get a job source adapter instance

Parameters:

  • source (Symbol, Object)

    Either a symbolic name to look up, or an adapter instance

Returns:

Raises:

  • (RuntimeError)

    If the source symbol cannot be resolved



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/newshound/jobs.rb', line 25

def source(source)
  return source unless source.is_a?(Symbol)

  if @registry.key?(source)
    return @registry[source].new
  end

  constant = constants.find { |c| c.to_s.gsub(/(?<!^)([A-Z])/, "_\\1").downcase == source.to_s }
  raise "Invalid job source: #{source}" unless constant

  const_get(constant).new
end