Sha256: c07d71a63d567a5e1530a83c61abc7701caa4a5dccfa34b11c4155e1ad53b62b

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

module Columns

  # Represents data for the tables found in schema.
  class Table

    # Public: Find the table names.
    #
    # Returns an Array of String.
    attr_reader :names

    # Public: Creates a new Table.
    #
    # schema - The db/schema.rb as a String.
    def initialize(schema)
      @schema_lines = schema.split("\n")
      @names = @schema_lines.map {|line| Regex.table_name(line) }.compact
    end

    # 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.
    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
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
columns-0.2.0 lib/columns/table.rb
columns-0.1.1 lib/columns/table.rb
columns-0.1.0 lib/columns/table.rb