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



23
24
25
26
27
28
# File 'lib/schema_to_scaffold/table.rb', line 23

def self.parse(table_data)
  return unless name = table_data[/table "([^"]+?)"/]
  name = $1
  atts = table_data.lines.to_a.select {|line| line =~ /t\.\w+/ }.map {|att| Attribute.parse att }
  Table.new(name, atts)
end

Instance Method Details

#to_script(target, migragion_flag) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/schema_to_scaffold/table.rb', line 14

def to_script(target,migragion_flag)
  attributes_list = attributes.map(&:to_script).reject{|x| x.nil? || x.empty?}.join(' ')
  script = []
  script << "rails generate #{target} #{modelize name} #{attributes_list}"
  script << " --no-migration" unless migragion_flag
  script << "\n\n"
  return script
end