Module: Rseed

Defined in:
lib/rseed.rb,
lib/rseed/adapter.rb,
lib/rseed/helpers.rb,
lib/rseed/version.rb,
lib/rseed/attribute.rb,
lib/rseed/converter.rb,
lib/rseed/processor.rb,
lib/rseed/csv_adapter.rb,
lib/rseed/hash_adapter.rb,
lib/generators/rseed/converter.rb,
lib/rseed/attribute_converters.rb

Defined Under Namespace

Modules: AttributeConverters, Generators Classes: Adapter, Attribute, Converter, CsvAdapter, HashAdapter, Processor, ProgressBarLogger, Railtie

Constant Summary collapse

VERSION =
"1.0.20"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loggerObject

Returns the value of attribute logger.



14
15
16
# File 'lib/rseed.rb', line 14

def logger
  @logger
end

Class Method Details

.from_csv(file, options = {}) ⇒ Object



62
63
64
65
# File 'lib/rseed/helpers.rb', line 62

def from_csv(file, options = {})
  options[:adapter] = :csv
  from_file(file, options)
end

.from_file(file, options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/rseed/helpers.rb', line 51

def from_file file, options = {}
  unless f = import_file(file)
    logger.error "Cannot locate file: ".red + file.to_s
    return false
  end
  p = Processor.new(options)
  return nil unless p
  p.adapter.file = f
  process_with_status_bar p, title: file
end

.from_hash(hash_or_array, options = {}) ⇒ Object



67
68
69
70
71
72
# File 'lib/rseed/helpers.rb', line 67

def from_hash hash_or_array, options = {}
  p = Processor.new(options.merge(adapter: :hash))
  return nil unless p
  p.adapter.data = hash_or_array
  process_with_status_bar p, title: "Hash"
end

.import_file(file) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/rseed/helpers.rb', line 74

def import_file(file)
  return file if File.exists? file
  # Try it relative to the rseed directory
  rseed_file = File.join(Rails.root, 'db', 'rseed', file)
  return rseed_file if File.exists? rseed_file
  # Try it relative to the Rails directory
  rails_file = File.join(Rails.root, file)
  return rails_file if File.exists? rails_file
  nil
end

.process_with_status_bar(processor, options = {}) ⇒ Object



18
19
20
21
22
23
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
# File 'lib/rseed/helpers.rb', line 18

def process_with_status_bar processor, options = {}
  title = options[:title] ? options[:title].dup : "Seed"
  title = "#{processor.converter.name.cyan} #{title.blue}"
  record_count = 0
  progress_bar = ProgressBar.create(starting_at: nil, total: nil, format: "#{"Preprocessing".magenta} %t <%B>", title: title, throttle_rate: 1)
  processor.logger = Logger.new(ProgressBarLogger.new(progress_bar))
  processor.deserialize do |status, result, meta|
    eta = meta ? meta[:eta] : nil
    eta = eta ? Time.at(eta).utc.strftime("%H:%M:%S") : "??:??"
    case status
      when :processing
        progress_bar.format "#{"Processing".yellow} %t <%B> %c/%C (#{eta.to_s.yellow})"
        if meta
          if record_count != meta[:record_count]
            record_count = meta[:record_count]
            progress_bar.total ||= meta[:total_records]
            # Set the progress unless it is the finishing record.
            progress_bar.progress = record_count unless record_count == progress_bar.total
          end
        end
      when :complete
        progress_bar.format "#{"Complete".green} %t <%B> %C (%a)"
        progress_bar.finish if progress_bar.total
      when :error
        processor.logger.error "Error during processing"
        processor.logger.error result[:message].to_s.red
        processor.logger.error meta.to_s.cyan if meta
        processor.logger.error result[:error]
        processor.logger.error result[:backtrace].join('\n') if result[:backtrace]
    end
  end
end