Sha256: c20f74d93a457bb555174c8d4e8da56bc69008c3510c3ac19230256053f44d58

Contents?: true

Size: 1.72 KB

Versions: 17

Compression:

Stored size: 1.72 KB

Contents

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

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

        extend Dry::Core::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 as(aliaz)
          Name[relation_name.relation, relation_name.dataset, aliaz]
        end

        def to_sym
          dataset
        end

        def sql_literal(ds)
          Sequel.expr(aliased? ? :"#{dataset}___#{key}" : dataset).sql_literal(ds)
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
rom-sql-1.3.1 lib/rom/sql/association/name.rb
rom-sql-1.3.0 lib/rom/sql/association/name.rb
rom-sql-1.2.2 lib/rom/sql/association/name.rb
rom-sql-1.2.1 lib/rom/sql/association/name.rb
rom-sql-1.2.0 lib/rom/sql/association/name.rb
rom-sql-1.1.2 lib/rom/sql/association/name.rb
rom-sql-1.1.1 lib/rom/sql/association/name.rb
rom-sql-1.1.0 lib/rom/sql/association/name.rb
rom-sql-1.0.3 lib/rom/sql/association/name.rb
rom-sql-1.0.2 lib/rom/sql/association/name.rb
rom-sql-1.0.1 lib/rom/sql/association/name.rb
rom-sql-1.0.0 lib/rom/sql/association/name.rb
rom-sql-1.0.0.rc2 lib/rom/sql/association/name.rb
rom-sql-1.0.0.rc1 lib/rom/sql/association/name.rb
rom-sql-1.0.0.beta3 lib/rom/sql/association/name.rb
rom-sql-1.0.0.beta2 lib/rom/sql/association/name.rb
rom-sql-1.0.0.beta1 lib/rom/sql/association/name.rb