Sha256: afc5732439e7f419e0aff9be50af3929b7b6a873384d28708935943777cfffbb

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

require 'dry/equalizer'
require 'rom/relation/name'
require 'rom/support/cache'

module ROM
  module SQL
    class Association
      class Name
        include Dry::Equalizer.new(:relation_name, :key)

        extend Cache

        attr_reader :relation_name

        attr_reader :key

        alias_method :to_sym, :key

        def self.[](*args)
          fetch_or_store(args) do
            rel, ds, aliaz = args

            if rel.is_a?(ROM::Relation::Name)
              new(rel, rel.dataset)
            elsif rel.is_a?(self)
              rel
            elsif aliaz
              new(ROM::Relation::Name[rel, ds], aliaz)
            elsif ds.nil?
              new(ROM::Relation::Name[rel], rel)
            else
              new(ROM::Relation::Name[rel, ds], ds)
            end
          end
        end

        def initialize(relation_name, aliaz)
          @relation_name = relation_name
          @aliased = relation_name.dataset != aliaz
          @key = aliased? ? aliaz : relation_name.dataset
        end

        def aliased?
          @aliased
        end

        def inspect
          if aliased?
            "#{self.class}(#{relation_name.to_s} as #{key})"
          else
            "#{self.class}(#{relation_name.to_s})"
          end
        end
        alias_method :to_s, :inspect

        def dataset
          relation_name.dataset
        end

        def relation
          relation_name.relation
        end

        def sql_literal_append(ds, sql)
          ds.literal_append(sql, dataset)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rom-sql-0.9.1 lib/rom/sql/association/name.rb
rom-sql-0.9.0 lib/rom/sql/association/name.rb
rom-sql-0.8.0 lib/rom/sql/association/name.rb