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

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

Class Method Summary collapse

Class Method Details

.decorableObject

Store which elements should be initialized



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

def decorable
  @decorable ||= Hash.new{ |h, k| h[k] = [] }
end

.decorable?(key, klass, attribute) ⇒ Boolean

Check whether the given attribute on the given klass is decorable by this type mapper

Returns:

  • (Boolean)


67
68
69
70
# File 'lib/torque/postgresql/attributes/type_map.rb', line 67

def decorable?(key, klass, attribute)
  key.class.auto_initialize? ||
    (decorable.key?(klass) && decorable[klass].include?(attribute.to_s))
end

.decorate(klass, *attributes, **set_options) ⇒ Object

Mark the list of attributes on the given class that can be decorated



24
25
26
27
28
29
# File 'lib/torque/postgresql/attributes/type_map.rb', line 24

def decorate(klass, *attributes, **set_options)
  attributes.flatten.each do |attribute|
    decorable[klass] << attribute.to_s
    options[klass][attribute.to_s] = set_options.deep_dup
  end
end

.decorate!(klass, *attributes, **options) ⇒ Object

Force the list of attributes on the given class to be decorated by this type mapper



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

def decorate!(klass, *attributes, **options)
  decorate(klass, *attributes, **options)
  attributes.flatten.map do |attribute|
    type = klass.attribute_types[attribute.to_s]
    lookup(type, klass, attribute.to_s)
  end
end

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

Search for a type match and process it if any



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/torque/postgresql/attributes/type_map.rb', line 48

def lookup(key, klass, attribute, *args)
  return unless present?(key) && decorable?(key, klass, attribute)

  set_options = options[klass][attribute]
  args.unshift(set_options) unless set_options.nil?
  klass.instance_exec(key, attribute, *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

.optionsObject

List of options for each individual attribute on each klass



19
20
21
# File 'lib/torque/postgresql/attributes/type_map.rb', line 19

def options
  @options ||= Hash.new{ |h, k| h[k] = {} }
end

.present?(key) ⇒ Boolean

Check if the given type class is registered

Returns:

  • (Boolean)


61
62
63
# File 'lib/torque/postgresql/attributes/type_map.rb', line 61

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

.raise_type_defined(key) ⇒ Object

Message when trying to define multiple types

Raises:

  • (ArgumentError)


73
74
75
76
77
# File 'lib/torque/postgresql/attributes/type_map.rb', line 73

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



42
43
44
45
# File 'lib/torque/postgresql/attributes/type_map.rb', line 42

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