Class: CaTissue::Extractor

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/catissue/extract/extractor.rb

Overview

Extracts caTissue objects.

Constant Summary collapse

DEF_NAME =

The default name of this migrator.

'caTissue Extractor'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Extractor

Creates a new Extractor with the given options.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :file (String)

    the extract configuration file name

  • :name (String)

    name of this Migrator (default is caTissue Migrator)

  • :output (String)

    optional output CSV file name

  • :target (String)

    required target domain class or class name

  • :ids (String)

    the database identifiers of the target objects to extract

  • :log (String)

    log file (default log/extract.log)

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/catissue/extract/extractor.rb', line 24

def initialize(options={})
  conf_file = options.delete(:file)
  if conf_file then
    CaRuby::Properties.new(conf_file, :array => [:shims]).each { |key, value| options[key.to_sym] ||= value }
  end
  # tailor the options
  name = options[:name] || DEF_NAME
  super(name)
  @ids = options[:ids]
  raise ArgumentError.new("Missing required ids option") unless @ids
  # convert the required target to a CaTissue class if necessary
  @target = target_class_from_option(options[:target])
  @target ||= CaTissue::Specimen
  # the CSV output file
  @output = options[:output]
  raise ArgumentError.new("Missing required extract output file option") unless @output
  # the field mapping configuration
  fld_conf = options[:mapping]
  mapper = CaRuby::CsvMapper.new(fld_conf, @target, @output, :mode => "w")
  @csvio = mapper.csvio
  @fld_path_hash = {}
  mapper.paths.each do |path|
    fld = mapper.header(path)
    # the path node is either an attribute symbol or attribute metadata;
    # if metadata, then use the reader method.
    @fld_path_hash[fld] = path.map { |attr_or_md| CaRuby::AttributeMetadata === attr_or_md ? attr_or_md.reader : attr_or_md }
  end
  logger.debug { "Extract field => path map: #{@fld_path_hash.transform { |path| path.join('.') }.pp_s}" }
end

Instance Attribute Details

#outputObject (readonly)

The extract output file



14
15
16
# File 'lib/catissue/extract/extractor.rb', line 14

def output
  @output
end

Instance Method Details

#extractObject Also known as: each

Executes this extractor CSV file and calls the block given to this method on each target domain object.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/catissue/extract/extractor.rb', line 64

def extract
  logger.debug { "Found #{@ids.size} extract targets." }
  CaTissue::Database.instance.open do
    @ids.each do |identifier|
      obj = @target.new(:identifier => identifier)
      logger.debug { "Finding extract target #{obj}..." }
      if obj.find then
        logger.debug { "Extractor fetched #{obj}." }
        yield obj
      else
        logger.debug { "Extract target #{obj} not found." }
      end
    end
  end
end

#runObject

Exports the selected target records from the database to the output file.



55
56
57
58
59
60
61
# File 'lib/catissue/extract/extractor.rb', line 55

def run
  begin
    extract { |obj| write(obj) }
  ensure
    @csvio.close
  end
end