Class: ScriptLogger
- Inherits:
-
Object
- Object
- ScriptLogger
- Defined in:
- lib/script_logger.rb
Instance Attribute Summary collapse
-
#entries ⇒ Object
Returns the value of attribute entries.
-
#extra_headers ⇒ Object
readonly
Returns the value of attribute extra_headers.
-
#log_name ⇒ Object
readonly
Returns the value of attribute log_name.
-
#target_obj_attributes ⇒ Object
readonly
Returns the value of attribute target_obj_attributes.
Class Method Summary collapse
Instance Method Summary collapse
- #entry_count ⇒ Object
-
#initialize(name, args) ⇒ ScriptLogger
constructor
A new instance of ScriptLogger.
- #log(entry) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(name, args) ⇒ ScriptLogger
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# 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 |
Instance Attribute Details
#entries ⇒ Object
Returns the value of attribute entries.
9 10 11 |
# File 'lib/script_logger.rb', line 9 def entries @entries end |
#extra_headers ⇒ Object (readonly)
Returns the value of attribute extra_headers.
8 9 10 |
# File 'lib/script_logger.rb', line 8 def extra_headers @extra_headers end |
#log_name ⇒ Object (readonly)
Returns the value of attribute log_name.
8 9 10 |
# File 'lib/script_logger.rb', line 8 def log_name @log_name end |
#target_obj_attributes ⇒ Object (readonly)
Returns the value of attribute target_obj_attributes.
8 9 10 |
# File 'lib/script_logger.rb', line 8 def target_obj_attributes @target_obj_attributes end |
Class Method Details
.all_logs_formatted ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# 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 "\n\#{output}\n" end |
Instance Method Details
#entry_count ⇒ Object
46 47 48 |
# File 'lib/script_logger.rb', line 46 def entry_count entries.count - 1 end |
#log(entry) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# 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 ⇒ Object
50 51 52 53 54 55 56 57 |
# 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" "\#{log_title.center(40,'-')}\n\#{output}\n" end |