module Filereader

A Filereader module which creates Records based on migration files

Public Class Methods

construct_record(filename) click to toggle source

Takes in a filename as input and constructs a Record

# File lib/active_recorder/filereader.rb, line 18
def self.construct_record(filename)
  # Get migration file information
  record_info = Filereader.get_initial_line(filename)
  # Check if file was a valid migration file
  fail ArgumentError, 'Not a migration file' if record_info.empty?
  columns_info = Filereader.get_columns(filename, record_info['var_name'])
  # Create a new record
  record = Record.new(record_info['record_name'])
  # Add hashed to record
  columns_info.each { |column, type| record.add_column(column, type) }
  record
end
construct_records(path) click to toggle source

Takes in directory with migrations and returns array of records created

# File lib/active_recorder/filereader.rb, line 4
def self.construct_records(path)
  # Initialize array of records
  records = []
  # Iterates through every file in a directory
  dir = File.join(path, 'db/migrate')
  Dir.foreach(dir) do |file|
    next if file.start_with?('.')
    # Append to records
    records << Filereader.construct_record(Filereader.to_absolute_path(dir, file))
  end
  records
end
get_columns(filename, var_name) click to toggle source

Takes in a filename and gets the column lines of the file

# File lib/active_recorder/filereader.rb, line 32
def self.get_columns(filename, var_name)
  # Initialize a column array
  columns = {}
  # Opens file and retrieve column lines
  File.open(filename).each do |line|
    # Strip line
    strs = line.strip!.split
    # Adds column lines hash
    if line.start_with?(var_name)
      strs[1] = strs[1].slice(0, strs[1].length - 1) if strs[1].end_with?(',')
      type = Filereader.to_data_type(strs[0], var_name)
      columns[Filereader.to_column_name(strs[1])] = type if type != 'timestamps'
    end
    # Check if reach end
    break if line.strip! == 'end'
  end
  columns
end
get_initial_line(filename) click to toggle source

Takes in a filename and gets the initial line information

# File lib/active_recorder/filereader.rb, line 52
def self.get_initial_line(filename)
  # Initialize a hash
  hash = {}
  # Opens file and retrieve initial line
  File.open(filename).each do |line|
    # Strip line
    strs = line.strip!.split
    # Adds initial line
    next unless !strs[0].nil? && strs[0].start_with?('create_table')
    # Check if we reached end
    hash['record_name'] = Filereader.to_record_name(strs[1])
    hash['var_name'] = Filereader.to_variable_name(strs[3])
    break
  end
  hash
end
to_absolute_path(dir, file) click to toggle source

Convert to absolute path

# File lib/active_recorder/filereader.rb, line 106
def self.to_absolute_path(dir, file)
  "#{dir}/#{file}"
end
to_association_record_name(name) click to toggle source

Get association table name

# File lib/active_recorder/filereader.rb, line 87
def self.to_association_record_name(name)
  split = name.split('_')
  name = "#{split[0].slice(1, split[0].length).capitalize}#{split[1].capitalize}"
  entity_name = name[0, name.length - 1]
  entity_name = name[0, name.length - 2] if name.end_with?('ses')
  entity_name
end
to_column_name(type) click to toggle source

Convert to data type

# File lib/active_recorder/filereader.rb, line 101
def self.to_column_name(type)
  type[1, type.length - 1]
end
to_data_type(col, var_name) click to toggle source

Convert to column name

# File lib/active_recorder/filereader.rb, line 96
def self.to_data_type(col, var_name)
  col[var_name.length + 1, col.length - var_name.length]
end
to_record_name(name) click to toggle source

Convert table name to Entity name

# File lib/active_recorder/filereader.rb, line 75
def self.to_record_name(name)
  entity_name = name[1, name.length - 2].capitalize
  # Check if name has '_'
  if name.include? '_'
    entity_name = Filereader.to_association_record_name(name)
  elsif name.end_with?('ses')
    entity_name = name[1, name.length - 3].capitalize
  end
  entity_name
end
to_variable_name(var) click to toggle source

Convert to variable name

# File lib/active_recorder/filereader.rb, line 70
def self.to_variable_name(var)
  var[1, var.length - 2]
end