Module: ROM::SQL::TypeExtensions

Defined in:
lib/rom/sql/type_extensions.rb

Overview

Type-specific methods

Class Method Summary collapse

Class Method Details

.[](type) ⇒ Hash

Gets extensions for a type

Parameters:

  • type (Dry::Types::Type)

    wrapped

Returns:

  • (Hash)


17
18
19
# File 'lib/rom/sql/type_extensions.rb', line 17

def [](type)
  @types[type.meta[:database]][type.meta[:db_type]] || EMPTY_HASH
end

.register(type, &block) ⇒ Object

Registers a set of operations supported for a specific type

Examples:

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

Parameters:

  • type (Dry::Types::Type)

    Type



33
34
35
36
37
38
39
40
41
# File 'lib/rom/sql/type_extensions.rb', line 33

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