Sha256: 83cda39bf1779610acf21355c059a192c9e8f5307fd3fd7430f33293e2cfe43e

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module TableSaw
  class ForeignKey
    class Column
      REGEX = /(\w+)(?::(\w+)\((\w+)\))?/

      attr_reader :value

      def initialize(value)
        @value = value
      end

      def primary_key
        value[REGEX, 1]
      end

      def type_condition
        polymorphic? ? "#{type_column} = '#{type_value}'" : '1 = 1'
      end

      private

      def type_column
        value[REGEX, 2]
      end

      def type_value
        value[REGEX, 3]
      end

      def polymorphic?
        !(type_column.nil? || type_value.nil?)
      end
    end

    attr_reader :name, :from_table, :from_column, :to_table, :to_column

    def initialize(from_table:, from_column:, to_table:, to_column:, name: nil)
      @name = name
      @from_table = from_table
      @from_column = from_column
      @to_table = to_table
      @to_column = to_column
    end

    def type_condition
      @type_condition ||= column.type_condition
    end

    def column
      @column ||= Column.new(from_column)
    end

    def eql?(other)
      hash == other.hash
    end

    def hash
      [from_table, from_column, to_table, to_column].hash
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
table_saw-3.2.0 lib/table_saw/foreign_key.rb
table_saw-3.1.0 lib/table_saw/foreign_key.rb
table_saw-3.0.0 lib/table_saw/foreign_key.rb