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



19
20
21
22
23
24
# File 'lib/schema_to_scaffold/table.rb', line 19

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) ⇒ Object



14
15
16
17
# File 'lib/schema_to_scaffold/table.rb', line 14

def to_script(target)
  return "rails generate #{target} #{modelize name} #{attributes.map(&:to_script).join(' ')}" if target == "scaffold"
  return "rails generate #{target} #{modelize name} #{attributes.map(&:to_script).join(' ')}" if target == "factory_girl:model"
end