Class: SchemaToScaffold::Schema

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Schema

Returns a new instance of Schema.



6
7
8
# File 'lib/schema_to_scaffold/schema.rb', line 6

def initialize(data)
  @data, @tables = data, Schema.parse(data)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/schema_to_scaffold/schema.rb', line 4

def data
  @data
end

#tablesObject (readonly)

Returns the value of attribute tables.



4
5
6
# File 'lib/schema_to_scaffold/schema.rb', line 4

def tables
  @tables
end

Class Method Details

.parse(data) ⇒ Object



40
41
42
# File 'lib/schema_to_scaffold/schema.rb', line 40

def self.parse(data)
  data.split(/^\s*create_/)[1..-1].map {|table_data| Table.parse(table_data) }.reject{ |e| e.nil? }
end

Instance Method Details



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

def print_table_names
  table_names.each_with_index { |name, i|  puts "#{i}. #{name}" }
end

#select_tables(input) ⇒ Object



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

def select_tables(input)
  case input
  when "*"
    table_range.to_a
  when /^\d/
    table_range.include?(input.to_i) ? [input.to_i] : []
  end
end

#table(id) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/schema_to_scaffold/schema.rb', line 27

def table(id)
  case id
  when Symbol then table(id.to_s)
  when String then tables[table_names.index(id)]
  when Integer then tables[id]
  else nil
  end
end

#table_namesObject



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

def table_names
  tables.map(&:name)
end

#to_scriptObject



36
37
38
# File 'lib/schema_to_scaffold/schema.rb', line 36

def to_script
  tables.map(&:to_script)
end