Class: SchemaToScaffold::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/schema_to_scaffold/table.rb

Overview

fetch table names and convert them to a scaffold syntax

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attributes) ⇒ Table

Returns a new instance of Table.



10
11
12
# File 'lib/schema_to_scaffold/table.rb', line 10

def initialize(name, attributes)
  @name, @attributes = name, attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



8
9
10
# File 'lib/schema_to_scaffold/table.rb', line 8

def attributes
  @attributes
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/schema_to_scaffold/table.rb', line 8

def name
  @name
end

Class Method Details

.parse(table_data) ⇒ Object



30
31
32
33
34
35
# 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

Instance Method Details

#to_script(target, migration_flag) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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