Class: ScriptLogger

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ ScriptLogger

Returns a new instance of ScriptLogger.



11
12
13
14
15
# File 'lib/script_logger.rb', line 11

def initialize(args)
  @log_name = args[:log_name]
  @headers = args[:headers]
  @entries = [] << CSV.generate_line(@headers).chomp
end

Instance Attribute Details

#entriesObject

Returns the value of attribute entries.



9
10
11
# File 'lib/script_logger.rb', line 9

def entries
  @entries
end

#headersObject (readonly)

Returns the value of attribute headers.



8
9
10
# File 'lib/script_logger.rb', line 8

def headers
  @headers
end

#log_nameObject (readonly)

Returns the value of attribute log_name.



8
9
10
# File 'lib/script_logger.rb', line 8

def log_name
  @log_name
end

Class Method Details

.all_logs_formattedObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/script_logger.rb', line 37

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

Instance Method Details

#entry_countObject



24
25
26
# File 'lib/script_logger.rb', line 24

def entry_count
  entries.count - 1
end

#log(entry) ⇒ Object



17
18
19
20
21
22
# File 'lib/script_logger.rb', line 17

def log(entry)
  unknown_entry_keys = entry.keys - headers
  raise "Unknown Entry Keys: #{unknown_entry_keys}" if unknown_entry_keys.any?
  ordered_entry = headers.map {|entry_key| entry.fetch(entry_key, nil)}
  entries << CSV.generate_line(ordered_entry).chomp
end

#to_sObject



28
29
30
31
32
33
34
35
# File 'lib/script_logger.rb', line 28

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