Class: Columns::Table

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

Overview

Public: Represents data for the tables found in schema.

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ Table

Public:

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



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

def initialize(schema)
  # Records the original schema file. TODO See if it's really needed.
  @schema = schema
  @schema_lines = schema.split("\n")
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.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/columns/table.rb', line 30

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

#namesObject

Public: Find the table names.

Returns an Array of String.



18
19
20
# File 'lib/columns/table.rb', line 18

def names
  @schema_lines.map {|line| Regex.table_name(line) }.compact
end