Class: Columns::Table

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

Overview

Represents data for the tables found in schema.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ Table

Public: Creates a new Table.

schema - The db/schema.rb as a String.



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

def initialize(schema)
  @schema_lines = schema.split("\n")
  @names = @schema_lines.map {|line| Regex.table_name(line) }.compact
end

Instance Attribute Details

#namesObject (readonly)

Public: Find the table names.

Returns an Array of String.



9
10
11
# File 'lib/columns/table.rb', line 9

def names
  @names
end

Instance Method Details

#content_for(name) ⇒ Object

Public: Get the column names and types for a given table.

name - The String name of the desired table.

Returns a String with the raw content of the given table section

from the schema file.

TODO Sure it’s better to parse the schema only once in #initialize.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/columns/table.rb', line 27

def content_for(name)
  found = false
  result = ''
  @schema_lines.each do |line|
    if Regex.table_name(line) == name
      found = true
      next
    end
    if found
      break if line =~ /\w*end$/
      result << line + "\n"
    end
  end
  result
end