Class: CaTissue::Extractor
- Inherits:
-
Object
- Object
- CaTissue::Extractor
- 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
-
#output ⇒ Object
readonly
The extract output file.
Instance Method Summary collapse
-
#extract ⇒ Object
(also: #each)
Executes this extractor CSV file and calls the block given to this method on each target domain object.
-
#initialize(options = {}) ⇒ Extractor
constructor
Creates a new Extractor with the given options.
-
#run ⇒ Object
Exports the selected target records from the database to the output file.
Constructor Details
#initialize(options = {}) ⇒ Extractor
Creates a new Extractor with the given options.
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(={}) conf_file = .delete(:file) if conf_file then CaRuby::Properties.new(conf_file, :array => [:shims]).each { |key, value| [key.to_sym] ||= value } end # tailor the options name = [:name] || DEF_NAME super(name) @ids = [: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([:target]) @target ||= CaTissue::Specimen # the CSV output file @output = [:output] raise ArgumentError.new("Missing required extract output file option") unless @output # the field mapping configuration fld_conf = [: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
#output ⇒ Object (readonly)
The extract output file
14 15 16 |
# File 'lib/catissue/extract/extractor.rb', line 14 def output @output end |
Instance Method Details
#extract ⇒ Object 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 |
#run ⇒ Object
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 |