Class: Columns::Table
- Inherits:
-
Object
- Object
- Columns::Table
- Defined in:
- lib/columns/table.rb
Overview
Represents data for the tables found in schema.
Instance Attribute Summary collapse
-
#names ⇒ Object
readonly
Public: Find the table names.
Instance Method Summary collapse
-
#content_for(name) ⇒ Object
Public: Get the column names and types for a given table.
-
#initialize(schema) ⇒ Table
constructor
Public: Creates a new Table.
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
#names ⇒ Object (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 |