class SchemaToScaffold::Table

fetch table names and convert them to a scaffold syntax

Attributes

attributes[R]
name[R]

Public Class Methods

new(name, attributes) click to toggle source
# File lib/schema_to_scaffold/table.rb, line 10
def initialize(name, attributes)
  @name, @attributes = name, attributes
end
parse(table_data) click to toggle source
# File lib/schema_to_scaffold/table.rb, line 30
def self.parse(table_data)
  return unless name = table_data[/table "([^"]+?)"/]
  name = $1
  table_fields = table_fields_of(table_data)
  Table.new(name, table_fields)
end

Private Class Methods

table_fields_of(table_data) click to toggle source
# File lib/schema_to_scaffold/table.rb, line 38
def self.table_fields_of(table_data)
  table_data.lines.to_a.select { |line| line =~ /t\.(?!index)\w+/ }.map { |att| Attribute.parse(att) }
end

Public Instance Methods

to_script(target, migration_flag) click to toggle source
# File lib/schema_to_scaffold/table.rb, line 14
def to_script(target, migration_flag)
  begin
    attributes_list = attributes.map(&:to_script).reject { |x| x.nil? || x.empty? }.join(' ')
  rescue => e
    puts "\n ---------------------------------------------"
    puts e.message
    puts "Table \n\n\n #{self.inspect} \n\n\n"
    puts "\n ---------------------------------------------"
  end
  script = []
  script << "rails generate #{target} #{modelize(name)} #{attributes_list}"
  script << " --no-migration" unless migration_flag
  script << "\n\n"
  script
end

Private Instance Methods

modelize(string) click to toggle source
# File lib/schema_to_scaffold/table.rb, line 44
def modelize(string)
  string.camelize.singularize
end