Module: Filereader
- Defined in:
- lib/active_recorder/filereader.rb
Overview
A Filereader module which creates Records based on migration files
Class Method Summary collapse
-
.construct_record(filename) ⇒ Object
Takes in a filename as input and constructs a Record.
-
.construct_records(path) ⇒ Object
Takes in directory with migrations and returns array of records created.
-
.get_columns(filename, var_name) ⇒ Object
Takes in a filename and gets the column lines of the file.
-
.get_initial_line(filename) ⇒ Object
Takes in a filename and gets the initial line information.
-
.to_absolute_path(dir, file) ⇒ Object
Convert to absolute path.
-
.to_association_record_name(name) ⇒ Object
Get association table name.
-
.to_column_name(type) ⇒ Object
Convert to data type.
-
.to_data_type(col, var_name) ⇒ Object
Convert to column name.
-
.to_record_name(name) ⇒ Object
Convert table name to Entity name.
-
.to_variable_name(var) ⇒ Object
Convert to variable name.
Class Method Details
.construct_record(filename) ⇒ Object
Takes in a filename as input and constructs a Record
18 19 20 21 22 23 24 25 26 27 28 29 |
# 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) ⇒ Object
Takes in directory with migrations and returns array of records created
4 5 6 7 8 9 10 11 12 13 14 15 |
# 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) ⇒ Object
Takes in a filename and gets the column lines of the file
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# 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) ⇒ Object
Takes in a filename and gets the initial line information
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# 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) ⇒ Object
Convert to absolute path
106 107 108 |
# File 'lib/active_recorder/filereader.rb', line 106 def self.to_absolute_path(dir, file) "#{dir}/#{file}" end |
.to_association_record_name(name) ⇒ Object
Get association table name
87 88 89 90 91 92 93 |
# 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) ⇒ Object
Convert to data type
101 102 103 |
# 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) ⇒ Object
Convert to column name
96 97 98 |
# 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) ⇒ Object
Convert table name to Entity name
75 76 77 78 79 80 81 82 83 84 |
# 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) ⇒ Object
Convert to variable name
70 71 72 |
# File 'lib/active_recorder/filereader.rb', line 70 def self.to_variable_name(var) var[1, var.length - 2] end |