Sha256: 98eda9f11a2e7a0c8da140fa05137ba221b4fe2336e979408d701304a21e8cdd

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

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

      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(name: nil, from_table:, from_column:, to_table:, to_column:)
      @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-2.8.1 lib/table_saw/foreign_key.rb
table_saw-2.8.0 lib/table_saw/foreign_key.rb
table_saw-2.7.0 lib/table_saw/foreign_key.rb