Sha256: 1f2e787d4f4aad6b656dc1d326e121f678af8ed8d93e0e1d5dc8a44307f8f31d

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

module ROM
  module SQL
    # Type-specific methods
    #
    # @api public
    module TypeExtensions
      class << self
        # Gets extensions for a type
        #
        # @param [Dry::Types::Type] wrapped
        #
        # @return [Hash]
        #
        # @api public
        def [](wrapped)
          type = wrapped.optional? ? wrapped.right : wrapped
          @types[type.meta[:database]][type.meta[:db_type]] || EMPTY_HASH
        end

        # Registers a set of operations supported for a specific type
        #
        # @example
        #   ROM::SQL::Attribute::TypeExtensions.register(ROM::SQL::Types::PG::JSONB) do
        #     def contain(type, expr, keys)
        #       Attribute[Types::Bool].meta(sql_expr: expr.pg_jsonb.contains(value))
        #     end
        #   end
        #
        # @param [Dry::Types::Type] type Type
        #
        # @api public
        def register(type, &block)
          extensions = @types[type.meta[:database]]
          db_type = type.meta[:db_type]

          raise ArgumentError, "Type #{ db_type } already registered" if extensions.key?(db_type)
          mod = Module.new(&block)
          ctx = Object.new.extend(mod)
          functions = mod.public_instance_methods.each_with_object({}) { |m, ms| ms[m] = ctx.method(m) }
          extensions[db_type] = functions
        end
      end

      @types = ::Hash.new do |hash, database|
        hash[database] = {}
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rom-sql-2.0.0.rc1 lib/rom/sql/type_extensions.rb