Module: Torque::PostgreSQL::Attributes::TypeMap

Defined in:
lib/torque/postgresql/attributes/type_map.rb

Class Method Summary collapse

Class Method Details

.lookup(key, klass, *args) ⇒ Object

Search for a type match and process it if any



20
21
22
23
24
25
26
27
# File 'lib/torque/postgresql/attributes/type_map.rb', line 20

def lookup(key, klass, *args)
  return unless present?(key)
  klass.instance_exec(key, *args, &types[key.class])
rescue LocalJumpError
  # There's a bug or misbehavior that blocks being called through
  # instance_exec don't accept neither return nor break
  return false
end

.present?(key) ⇒ Boolean

Check if the given type class is registered

Returns:

  • (Boolean)


30
31
32
# File 'lib/torque/postgresql/attributes/type_map.rb', line 30

def present?(key)
  types.key?(key.class)
end

.raise_type_defined(key) ⇒ Object

Message when trying to define multiple types

Raises:

  • (ArgumentError)


35
36
37
38
39
# File 'lib/torque/postgresql/attributes/type_map.rb', line 35

def raise_type_defined(key)
  raise ArgumentError, <<-MSG.strip
    Type #{key} is already defined here: #{types[key].source_location.join(':')}
  MSG
end

.register_type(key, &block) ⇒ Object

Register a type that can be processed by a given block



14
15
16
17
# File 'lib/torque/postgresql/attributes/type_map.rb', line 14

def register_type(key, &block)
  raise_type_defined(key) if present?(key)
  types[key] = block
end

.typesObject

Reader of the list of tyes



9
10
11
# File 'lib/torque/postgresql/attributes/type_map.rb', line 9

def types
  @types ||= {}
end