class ScriptLogger

Attributes

entries[RW]
extra_headers[R]
log_name[R]
target_obj_attributes[R]

Public Class Methods

all_logs_formatted() click to toggle source
# File lib/script_logger.rb, line 59
  def self.all_logs_formatted
    output = ObjectSpace.each_object(self).inject("") do |all_logs,log|
      all_logs += "#{log}\n\n"
    end

    <<-STR

#{output}
STR
  end
new(name, args) click to toggle source
# File lib/script_logger.rb, line 11
def initialize(name, args)
  @log_name = name
  @obj_headers = []
  @target_obj_attributes = {}

  args.each do |obj_name,obj_attrs|
    next if obj_name == :extra_headers

    @target_obj_attributes[obj_name] = []
    obj_attrs.each do |column_info|
      target_attr = column_info.is_a?(Hash) ? column_info.keys.first : column_info
      @target_obj_attributes[obj_name] << target_attr
    end

    obj_attrs.each do |column_info|
      column_name = column_info.is_a?(Hash) ? column_info.values.first : "#{obj_name}_#{column_info}"
      @obj_headers << column_name
    end
  end

  @extra_headers = args.fetch(:extra_headers,[])
  @entries       = [] << CSV.generate_line(@obj_headers + @extra_headers).chomp
end

Public Instance Methods

entry_count() click to toggle source
# File lib/script_logger.rb, line 46
def entry_count
  entries.count - 1
end
log(entry) click to toggle source
# File lib/script_logger.rb, line 35
def log(entry)
  entry_build = []
  entry.each do |col_key,col_val|
    if obj_attrs = target_obj_attributes[col_key]
      entry_build += obj_attrs.map {|obj_attr| col_val.send(obj_attr) }
    end
  end
  entry_build += extra_headers.map {|column| entry.fetch(column, nil)}
  entries << CSV.generate_line(entry_build).chomp
end
to_s() click to toggle source
# File lib/script_logger.rb, line 50
  def to_s
    output = entries.inject("") { |r,entry| r += "#{entry}\n" }
    log_title = "#{log_name} -- #{entry_count} records"
    <<-STR
#{log_title.center(40,'-')}
#{output}
STR
  end