Class: Drudgery::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/drudgery/job.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Job

Returns a new instance of Job.



6
7
8
9
10
11
12
13
14
# File 'lib/drudgery/job.rb', line 6

def initialize(options={})
  @id           = Time.now.nsec
  @extractor    = options[:extractor]
  @loader       = options[:loader]
  @transformer  = options[:transformer]
  @batch_size   = options[:batch_size] || 1000

  @records = []
end

Instance Attribute Details

#batch_sizeObject

Returns the value of attribute batch_size.



4
5
6
# File 'lib/drudgery/job.rb', line 4

def batch_size
  @batch_size
end

#completed_atObject (readonly)

Returns the value of attribute completed_at.



3
4
5
# File 'lib/drudgery/job.rb', line 3

def completed_at
  @completed_at
end

#extractorObject

Returns the value of attribute extractor.



4
5
6
# File 'lib/drudgery/job.rb', line 4

def extractor
  @extractor
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/drudgery/job.rb', line 3

def id
  @id
end

#loaderObject

Returns the value of attribute loader.



4
5
6
# File 'lib/drudgery/job.rb', line 4

def loader
  @loader
end

#started_atObject (readonly)

Returns the value of attribute started_at.



3
4
5
# File 'lib/drudgery/job.rb', line 3

def started_at
  @started_at
end

#transformerObject

Returns the value of attribute transformer.



4
5
6
# File 'lib/drudgery/job.rb', line 4

def transformer
  @transformer
end

Instance Method Details

#extract(*args) {|extractor| ... } ⇒ Object

Yields:



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

def extract(*args)
  if args.first.kind_of?(Symbol)
    extractor = Drudgery::Extractors.instantiate(*args)
  else
    extractor = args.first
  end

  yield extractor if block_given?

  @extractor = extractor
end

#load(*args) {|loader| ... } ⇒ Object

Yields:



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/drudgery/job.rb', line 44

def load(*args)
  if args.first.kind_of?(Symbol)
    loader = Drudgery::Loaders.instantiate(*args)
  else
    loader = args.first
  end

  yield loader if block_given?

  @loader = loader
end

#nameObject



16
17
18
# File 'lib/drudgery/job.rb', line 16

def name
  "#{@extractor.name} => #{@loader.name}"
end

#performObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/drudgery/job.rb', line 56

def perform
  @started_at = Time.now
  Drudgery.notify :before_job, self

  extract_records do |record|
    @records << record

    if @records.size == @batch_size
      load_records
    end
  end

  load_records

  @completed_at = Time.now
  Drudgery.notify :after_job, self
end

#record_countObject



20
21
22
23
24
# File 'lib/drudgery/job.rb', line 20

def record_count
  if @extractor
    @record_count ||= @extractor.record_count
  end
end

#transform(transformer = Drudgery::Transformer.new, &processor) ⇒ Object



38
39
40
41
42
# File 'lib/drudgery/job.rb', line 38

def transform(transformer=Drudgery::Transformer.new, &processor)
  transformer.register(processor) if processor

  @transformer = transformer
end