Class: RStore::Logger

Inherits:
Object show all
Defined in:
lib/rstore/logger.rb

Constant Summary collapse

KnownStates =
{:fetch   => "loading files",
:parse   => "parsing file content",
:convert => "converting field values into their corresponding datatypes",
:store   => "storing file content into database"}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_object) ⇒ Logger

Returns a new instance of Logger.



20
21
22
23
# File 'lib/rstore/logger.rb', line 20

def initialize data_object
  @data    = data_object
  @message = ''
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#messageObject

Returns the value of attribute message.



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

def message
  @message
end

Instance Method Details

#correct_location(location) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rstore/logger.rb', line 65

def correct_location location

  if location[:row]        # row_index
    row = correct_row(location[:row])
    if location[:col]      # col_index
      col = location[:col]+1
      {row: row, col: col}
    else
      {row: row}
    end
  else
    location
  end
end

#correct_row(row) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/rstore/logger.rb', line 81

def correct_row row
  # row = row_index, which starts at 0
  # Without headers: add 1 to row
  # With headers   : add another 1 to row as the header row had been already removed
  row = with_headers? ? row+2 : row+1
  row
end

#errorObject



52
53
54
# File 'lib/rstore/logger.rb', line 52

def error
  raise FileProcessingError, @message
end

#location_to_s(location) ⇒ Object

Helper methods ————————



59
60
61
# File 'lib/rstore/logger.rb', line 59

def location_to_s location
  location.map { |loc,val| "#{loc} #{val}" }.join(', ')
end

#log(state, error, loc = {}) ⇒ Object



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/rstore/logger.rb', line 26

def log state, error, loc={}
  raise ArgumentError "#{state} is an invalid state for #{self.class}"  unless valid_state? state

  loc = correct_location(loc)

  type_of_error = error.class
  error_message = error.to_s
  location      = "Location     : #{location_to_s(loc)}"
  location      = loc.empty? ? '' : location

  report = <<-TEXT.gsub(/^\s+/, '')
  An error occured while #{KnownStates[state]}:
  File         : #{@data.path}
  Type of error: #{type_of_error}
  Error message: #{error_message}
  #{location}
  =============
  Please fix the error and run again.
  NOTE: No data has been inserted into the database yet.
  =============
  TEXT

  @message = report
end

#valid_state?(state) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/rstore/logger.rb', line 90

def valid_state? state
  KnownStates.keys.any? { |val| val == state }
end

#with_headers?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/rstore/logger.rb', line 95

def with_headers?
  @data.options[:has_headers]
end