Method: Sequel::Postgres::PGMultiRange::DatabaseMethods#register_multirange_type

Defined in:
lib/sequel/extensions/pg_multirange.rb

#register_multirange_type(db_type, opts = OPTS, &block) ⇒ Object

Register a database specific multirange type. This can be used to support different multirange types per Database. Options:

:converter

A callable object (e.g. Proc), that is called with the PostgreSQL range string, and should return a PGRange instance.

:oid

The PostgreSQL OID for the multirange type. This is used by Sequel to set up automatic type conversion on retrieval from the database.

:range_oid

Should be the PostgreSQL OID for the multirange subtype (the range type). If given, automatically sets the :converter option by looking for scalar conversion proc.

If a block is given, it is treated as the :converter option.

Raises:



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/sequel/extensions/pg_multirange.rb', line 179

def register_multirange_type(db_type, opts=OPTS, &block)
  oid = opts[:oid]
  soid = opts[:range_oid]

  if has_converter = opts.has_key?(:converter)
    raise Error, "can't provide both a block and :converter option to register_multirange_type" if block
    converter = opts[:converter]
  else
    has_converter = true if block
    converter = block
  end

  unless (soid || has_converter) && oid
    range_oid, subtype_oid = from(:pg_range).join(:pg_type, :oid=>:rngmultitypid).where(:typname=>db_type.to_s).get([:rngmultitypid, :rngtypid])
    soid ||= subtype_oid unless has_converter
    oid ||= range_oid
  end

  db_type = db_type.to_s.dup.freeze

  if soid
    raise Error, "can't provide both a converter and :range_oid option to register" if has_converter 
    raise Error, "no conversion proc for :range_oid=>#{soid.inspect} in conversion_procs" unless converter = conversion_procs[soid]
  end

  raise Error, "cannot add a multirange type without a convertor (use :converter or :range_oid option or pass block)" unless converter
  creator = Creator.new(db_type, converter)
  add_conversion_proc(oid, creator)

  @pg_multirange_schema_types[db_type] = db_type.to_sym

  singleton_class.class_eval do
    meth = :"typecast_value_#{db_type}"
    scalar_typecast_method = :"typecast_value_#{opts.fetch(:scalar_typecast, db_type.sub('multirange', 'range'))}"
    define_method(meth){|v| typecast_value_pg_multirange(v, creator, scalar_typecast_method)}
    private meth
  end

  @schema_type_classes[db_type] = PGMultiRange
  nil
end