Sha256: a4dcabb4388dbe31251a56c75ac1645c77ec9b7d343ddf38f7c66a7e75d218fb

Contents?: true

Size: 1.42 KB

Versions: 8

Compression:

Stored size: 1.42 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.default? ? wrapped.type : wrapped
          type = type.optional? ? type.right : 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

8 entries across 8 versions & 1 rubygems

Version Path
rom-sql-3.0.1 lib/rom/sql/type_extensions.rb
rom-sql-3.0.0 lib/rom/sql/type_extensions.rb
rom-sql-2.5.0 lib/rom/sql/type_extensions.rb
rom-sql-2.4.0 lib/rom/sql/type_extensions.rb
rom-sql-2.3.0 lib/rom/sql/type_extensions.rb
rom-sql-2.2.1 lib/rom/sql/type_extensions.rb
rom-sql-2.2.0 lib/rom/sql/type_extensions.rb
rom-sql-2.1.0 lib/rom/sql/type_extensions.rb