Sha256: 51f02926b2356d5d5892ec128b9b2abbc1c1beb8e0cb1dbc4f78d82489f052bb

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

module Torque
  module PostgreSQL
    class AuxiliaryStatement
      class Settings < Collector.new(:attributes, :join, :join_type, :query, :requires,
                                     :polymorphic)

        attr_reader :source
        alias cte source

        delegate :base, :base_name, :base_table, :table, :table_name, to: :@source
        delegate :relation_query?, to: Torque::PostgreSQL::AuxiliaryStatement

        def initialize(source)
          @source = source
        end

        # Get the arel version of the table set on the query
        def query_table
          raise StandardError, 'The query is not defined yet' if query.nil?
          return query.arel_table if relation_query?(query)
          @query_table
        end

        # There are two ways of setting the query:
        # - A simple relation based on a modle
        # - A string or a proc that requires the table name as first argument
        def query(value = nil, command = nil)
          return @query if value.nil?
          return @query = value if relation_query?(value)

          valid_type = command.respond_to?(:call) || command.is_a?(String)
          raise ArgumentError, <<-MSG.strip.gsub(/\n +/, ' ') if command.nil?
            To use proc or string as query, you need to provide the table name
            as the first argument
          MSG
          raise ArgumentError, <<-MSG.strip.gsub(/\n +/, ' ') unless valid_type
            Only relation, string and proc are valid object types for query,
            #{command.inspect} given.
          MSG

          @query = command
          @query_table = Arel::Table.new(value)
        end

      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
torque-postgresql-0.1.2 lib/torque/postgresql/auxiliary_statement/settings.rb
torque-postgresql-0.1.1 lib/torque/postgresql/auxiliary_statement/settings.rb