Sha256: 95fc1ef7220a8b153de0dfc4b0d5bde2fd8480ed299df98fa99b6030d97130b8

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

require 'rom/support/inflector'
require 'rom/constants'

module ROM
  module SQL
    # @api private
    class DSL < BasicObject
      # @!attribute [r] schema
      #   @return [SQL::Schema]
      attr_reader :schema

      # @!attribute [r] relations
      #   @return [Hash, RelationRegistry]
      attr_reader :relations

      # @api private
      def initialize(schema)
        @schema = schema
        @relations = schema.respond_to?(:relations) ? schema.relations : EMPTY_HASH
      end

      # @api private
      def call(&block)
        result = instance_exec(relations, &block)

        if result.is_a?(::Array)
          result
        else
          [result]
        end
      end

      # Return a string literal that will be used directly in an ORDER clause
      #
      # @param [String] value
      #
      # @return [Sequel::LiteralString]
      #
      # @api public
      def `(value)
        ::Sequel.lit(value)
      end

      # @api private
      def respond_to_missing?(name, include_private = false)
        super || schema.key?(name)
      end

      private

      # @api private
      def type(identifier)
        type_name = Inflector.classify(identifier)
        types.const_get(type_name) if types.const_defined?(type_name)
      end

      # @api private
      def types
        ::ROM::SQL::Types
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rom-sql-2.5.0 lib/rom/sql/dsl.rb
rom-sql-2.4.0 lib/rom/sql/dsl.rb
rom-sql-2.3.0 lib/rom/sql/dsl.rb