Sha256: d68d0d2f06ff3bc2365f4b6f8d63a8cd4e6441ce26d4940f4dc6e1cb223bd805

Contents?: true

Size: 1.23 KB

Versions: 21

Compression:

Stored size: 1.23 KB

Contents

module Torque
  module PostgreSQL
    module Attributes
      module TypeMap

        class << self

          # Reader of the list of tyes
          def types
            @types ||= {}
          end

          # Register a type that can be processed by a given block
          def register_type(key, &block)
            raise_type_defined(key) if present?(key)
            types[key] = block
          end

          # Search for a type match and process it if any
          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

          # Check if the given type class is registered
          def present?(key)
            types.key?(key.class)
          end

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

        end

      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
torque-postgresql-0.2.13 lib/torque/postgresql/attributes/type_map.rb
torque-postgresql-0.2.12 lib/torque/postgresql/attributes/type_map.rb
torque-postgresql-0.2.11 lib/torque/postgresql/attributes/type_map.rb
torque-postgresql-0.2.10 lib/torque/postgresql/attributes/type_map.rb
torque-postgresql-0.2.9 lib/torque/postgresql/attributes/type_map.rb
torque-postgresql-0.2.8 lib/torque/postgresql/attributes/type_map.rb
torque-postgresql-0.2.7 lib/torque/postgresql/attributes/type_map.rb
torque-postgresql-0.2.6 lib/torque/postgresql/attributes/type_map.rb
torque-postgresql-0.2.5 lib/torque/postgresql/attributes/type_map.rb
torque-postgresql-0.2.4 lib/torque/postgresql/attributes/type_map.rb
torque-postgresql-0.2.3 lib/torque/postgresql/attributes/type_map.rb
torque-postgresql-0.2.2 lib/torque/postgresql/attributes/type_map.rb
torque-postgresql-0.2.1 lib/torque/postgresql/attributes/type_map.rb
torque-postgresql-0.1.7 lib/torque/postgresql/attributes/type_map.rb
torque-postgresql-0.1.6 lib/torque/postgresql/attributes/type_map.rb
torque-postgresql-0.1.5 lib/torque/postgresql/attributes/type_map.rb
torque-postgresql-0.1.4 lib/torque/postgresql/attributes/type_map.rb
torque-postgresql-0.1.3 lib/torque/postgresql/attributes/type_map.rb
torque-postgresql-0.1.2 lib/torque/postgresql/attributes/type_map.rb
torque-postgresql-0.1.1 lib/torque/postgresql/attributes/type_map.rb