Sha256: b25a47a8362aa5727d7ed767fff04b89de129fa80f5fc9a355b68d4e3b543d09

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module ROM
  module SQL
    # Type-specific methods
    #
    # @api public
    module TypeExtensions
      class << self
        # Gets extensions for a type
        #
        # @param type [Dry::Types::Type] wrapped
        #
        # @return [Hash]
        #
        # @api public
        def [](type)
          @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, &)
          extensions = @types[type.meta[:database]]
          db_type = type.meta[:db_type]

          mod = ::Module.new(&)
          ctx = ::Object.new.extend(mod)
          functions = mod.public_instance_methods.to_h { |m| [m, ctx.method(m)] }
          extensions[db_type] = (extensions[db_type] || {}).merge(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-3.7.0 lib/rom/sql/type_extensions.rb