Sha256: 9532a4cdf7f5a83b9b707c85d3a7e40806761d57c0a72f64387b8838ed89da77

Contents?: true

Size: 1.34 KB

Versions: 12

Compression:

Stored size: 1.34 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, &block)
          extensions = @types[type.meta[:database]]
          db_type = type.meta[: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] = (extensions[db_type] || {}).merge(functions)
        end
      end

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

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rom-sql-3.6.5 lib/rom/sql/type_extensions.rb
rom-sql-3.6.4 lib/rom/sql/type_extensions.rb
rom-sql-3.6.3 lib/rom/sql/type_extensions.rb
rom-sql-3.6.2 lib/rom/sql/type_extensions.rb
rom-sql-3.6.1 lib/rom/sql/type_extensions.rb
rom-sql-3.6.0 lib/rom/sql/type_extensions.rb
rom-sql-3.5.0 lib/rom/sql/type_extensions.rb
rom-sql-3.4.0 lib/rom/sql/type_extensions.rb
rom-sql-3.3.3 lib/rom/sql/type_extensions.rb
rom-sql-3.3.2 lib/rom/sql/type_extensions.rb
rom-sql-3.3.1 lib/rom/sql/type_extensions.rb
rom-sql-3.3.0 lib/rom/sql/type_extensions.rb