Module: Torque::PostgreSQL::Attributes::TypeMap
- Defined in:
- lib/torque/postgresql/attributes/type_map.rb
Class Method Summary collapse
-
.decorable ⇒ Object
Store which elements should be initialized.
-
.decorable?(key, klass, attribute) ⇒ Boolean
Check whether the given attribute on the given klass is decorable by this type mapper.
-
.decorate(klass, *attributes, **set_options) ⇒ Object
Mark the list of attributes on the given class that can be decorated.
-
.decorate!(klass, *attributes, **options) ⇒ Object
Force the list of attributes on the given class to be decorated by this type mapper.
-
.lookup(key, klass, attribute, *args) ⇒ Object
Search for a type match and process it if any.
-
.options ⇒ Object
List of options for each individual attribute on each klass.
-
.present?(key) ⇒ Boolean
Check if the given type class is registered.
-
.raise_type_defined(key) ⇒ Object
Message when trying to define multiple types.
-
.register_type(key, &block) ⇒ Object
Register a type that can be processed by a given block.
-
.types ⇒ Object
Reader of the list of tyes.
Class Method Details
.decorable ⇒ Object
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
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, **) attributes.flatten.each do |attribute| decorable[klass] << attribute.to_s [klass][attribute.to_s] = .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, **) decorate(klass, *attributes, **) 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) = [klass][attribute] args.unshift() unless .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 |
.options ⇒ Object
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 ||= Hash.new{ |h, k| h[k] = {} } end |
.present?(key) ⇒ Boolean
Check if the given type class is registered
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
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 |
.types ⇒ Object
Reader of the list of tyes
9 10 11 |
# File 'lib/torque/postgresql/attributes/type_map.rb', line 9 def types @types ||= {} end |