Class: Scrubber::Job

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name, options = {}) ⇒ Job

Returns a new instance of Job.



4
5
6
7
8
9
10
11
# File 'lib/scrubber/job.rb', line 4

def initialize(table_name, options = {})
  @table_name = table_name.to_s 
  unless options.empty?
    @conditional_proc_type = options.has_key?(:unless) ? :unless : :if
    @conditional_proc      = options[:unless]
    @field_transforms      = []
  end
end

Instance Attribute Details

#conditional_procObject (readonly)

Returns the value of attribute conditional_proc.



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

def conditional_proc
  @conditional_proc
end

#conditional_proc_typeObject (readonly)

Returns the value of attribute conditional_proc_type.



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

def conditional_proc_type
  @conditional_proc_type
end

#field_transformsObject (readonly)

Returns the value of attribute field_transforms.



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

def field_transforms
  @field_transforms
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



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

def table_name
  @table_name
end

Instance Method Details

#performObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/scrubber/job.rb', line 17

def perform
  #turn off timestamps (we want to preserve original timestamps)
  prior_setting = record_activerecord_timestamps(false)
  table_class.find_in_batches do |batch|
    batch.each do |record|
      if self.conditional_proc_type == :unless
        unless @conditional_proc.call(record)
          perform_scrubs_for(record)
        end
      elsif self.conditional_proc_type == :if
        if @conditional_proc.call(record)
          perform_scrubs_for(record)
        end
      else
        perform_scrubs_for(record)
      end
    end
  end
  #restore timestamping to prior state
  record_activerecord_timestamps(prior_setting)
end

#scrub(field, options) ⇒ Object



13
14
15
# File 'lib/scrubber/job.rb', line 13

def scrub(field, options)
  @field_transforms << Scrubber::FieldTransform.new(field, options) 
end