Class: RapidTransit::Base

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.actionsObject

Get the actions list



86
87
88
# File 'lib/rapid_transit/base.rb', line 86

def actions
  @actions
end

.column_namesObject

Returns the value of attribute column_names.



6
7
8
# File 'lib/rapid_transit/base.rb', line 6

def column_names
  @column_names
end

.delimiterObject

Returns the value of attribute delimiter.



6
7
8
# File 'lib/rapid_transit/base.rb', line 6

def delimiter
  @delimiter
end

.error_handlerObject

Returns the value of attribute error_handler.



6
7
8
# File 'lib/rapid_transit/base.rb', line 6

def error_handler
  @error_handler
end

Returns the value of attribute print_status.



6
7
8
# File 'lib/rapid_transit/base.rb', line 6

def print_status
  @print_status
end

.stripObject

Returns the value of attribute strip.



6
7
8
# File 'lib/rapid_transit/base.rb', line 6

def strip
  @strip
end

Class Method Details

.after_parseObject

Callback executed after parsing the file



125
# File 'lib/rapid_transit/base.rb', line 125

def self.after_parse; end

.before_parseObject

Callback executed before parsing the file



122
# File 'lib/rapid_transit/base.rb', line 122

def self.before_parse; end

.columns(*list) ⇒ Object

Define the list of columns expected in the CSV file

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
# File 'lib/rapid_transit/base.rb', line 47

def self.columns(*list)
  msg = 'Columns must be passed as Symbols'
  raise ArgumentError, msg, caller unless list.all? { |i| i.is_a? Symbol }
  msg = 'Column names must be unique'
  raise ArgumentError, msg, caller unless list.uniq == list
  @column_names = list
end

.define_action(action_class, method) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/rapid_transit/base.rb', line 55

def self.define_action(action_class, method)
  class_eval <<-EOS
    def self.#{method}(key, *args, &block)
      self.actions.add #{action_class}.new(:#{method}, key, *args, &block)
    end
  EOS
end

.delimit(delimiter) ⇒ Object

Set the delimiter used in the file



11
12
13
# File 'lib/rapid_transit/base.rb', line 11

def self.delimit(delimiter)
  @delimiter = delimiter
end

.exec(*args, &block) ⇒ Object

Define the setter method for setting a value in the row hash



81
82
83
# File 'lib/rapid_transit/base.rb', line 81

def self.exec(*args, &block)
  self.actions.add RapidTransit::Action::Base.new(:module_exec, *args, &block)
end

.on_error(&block) ⇒ Object

Set a block to execute on error

Raises:

  • (ArgumentError)


36
37
38
39
40
# File 'lib/rapid_transit/base.rb', line 36

def self.on_error(&block)
  msg = 'Error handler must be a block that accepts two parameters'
  raise ArgumentError, msg, caller unless block_given? && block.arity == 2
  @error_handler = block
end

.parse(file) ⇒ Object

Parse a CSV file



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rapid_transit/base.rb', line 91

def self.parse(file)
  raise RapidTransit::NoFileError, "File is blank", caller if file.blank?
  total = file.count
  file.rewind
  cr = "\r"
  clear = "\e[0K"
  reset = cr + clear
  before_parse
  count = 0
  file.each_line do |line|
    count += 1
    row = RapidTransit::Row.new(self, line, count)
    begin
      row.parse
    rescue => e
      error_handler.call e, row
    end
    if print_status
      print "#{reset}Parsed #{count} of #{total} records"
      STDOUT.flush
    end
  end
  after_parse
  if print_status
    print reset
    STDOUT.flush
  end
  count
end

.set(key, &block) ⇒ Object

Define the setter method for setting a value in the row hash define_action RapidTransit::Action::Setter, :set



75
76
77
# File 'lib/rapid_transit/base.rb', line 75

def self.set(key, &block)
  self.actions.add RapidTransit::Action::Setter.new(:set, key, &block)
end

.strip_columns(strip_val) ⇒ Object

Set whether to strip whitespace from column values



25
26
27
# File 'lib/rapid_transit/base.rb', line 25

def self.strip_columns(strip_val)
  @strip = strip_val
end