Sha256: 183fc0c329e67ed4df94f07a7edc9230a56779e258f089c16cd83b85f6603529

Contents?: true

Size: 713 Bytes

Versions: 1

Compression:

Stored size: 713 Bytes

Contents

require_relative "../columns"
require_relative "paths"

module Arable::Columns::FromSchema
  module_function

  def call(table_name)
    return if schema.blank?

    table_definition_match = schema.match(/create_table "#{table_name}".*?\n(.*?)\n *end/m)

    columns =
      table_definition_match[1]
        .lines
        .reject { |line| line.include?("t.index") }
        .map { |line| line.match(/t\.[a-z]* "([a-z_]*)"/)[1] }

    return columns if table_definition_match[0].include?("id: false")

    ["id"] + columns
  end

  private

  def self.schema
    @schema ||= begin
      return unless File.exist?(Arable::Columns::Paths::SCHEMA)

      File.read(Arable::Columns::Paths::SCHEMA)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
arable-0.2.0 lib/arable/columns/from_schema.rb