class AMA::Entity::Mapper::Type::Registry

Holds all registered types

Attributes

types[RW]

Public Class Methods

new() click to toggle source
# File lib/ama-entity-mapper/type/registry.rb, line 24
def initialize
  @types = {}
end

Public Instance Methods

[](klass) click to toggle source

@param [Class, Module] klass

# File lib/ama-entity-mapper/type/registry.rb, line 44
def [](klass)
  @types[klass]
end
find(klass) click to toggle source

@param [Class, Module] klass @return [AMA::Entity::Mapper::Type, NilClass]

# File lib/ama-entity-mapper/type/registry.rb, line 71
def find(klass)
  candidates = select(klass)
  candidates.empty? ? nil : candidates.first
end
find!(klass) click to toggle source

@param [Class, Module] klass @return [AMA::Entity::Mapper::Type]

# File lib/ama-entity-mapper/type/registry.rb, line 78
def find!(klass)
  candidate = find(klass)
  return candidate if candidate
  message = "Could not find any registered type for class #{klass}"
  compliance_error(message)
end
key?(klass) click to toggle source

@param [Class] klass

# File lib/ama-entity-mapper/type/registry.rb, line 54
def key?(klass)
  @types.key?(klass)
end
Also aliased as: registered?
register(type) click to toggle source

@param [AMA::Entity::Mapper::Type] type

# File lib/ama-entity-mapper/type/registry.rb, line 49
def register(type)
  @types[type.type] = type
end
registered?(klass)
Alias for: key?
resolvable?(klass) click to toggle source

@param [Class, Module] klass @return [TrueClass, FalseClass]

# File lib/ama-entity-mapper/type/registry.rb, line 87
def resolvable?(klass)
  !select(klass).empty?
end
select(klass) click to toggle source

@param [Class, Module] klass @return [Array<AMA::Entity::Mapper::Type>]

# File lib/ama-entity-mapper/type/registry.rb, line 62
def select(klass)
  types = class_hierarchy(klass).map do |entry|
    @types[entry]
  end
  types.reject(&:nil?)
end
with_default_types() click to toggle source

@return [AMA::Entity::Mapper::Type::Registry]

# File lib/ama-entity-mapper/type/registry.rb, line 29
def with_default_types
  register(BuiltIn::EnumerableType::INSTANCE)
  register(BuiltIn::ArrayType::INSTANCE)
  register(BuiltIn::HashType::INSTANCE)
  register(BuiltIn::SetType::INSTANCE)
  register(BuiltIn::HashTupleType::INSTANCE)
  register(BuiltIn::RationalType::INSTANCE)
  register(BuiltIn::DateTimeType::INSTANCE)
  BuiltIn::PrimitiveType::ALL.each do |type|
    register(type)
  end
  self
end

Private Instance Methods

class_hierarchy(klass) click to toggle source

@param [Class, Module] klass

# File lib/ama-entity-mapper/type/registry.rb, line 94
def class_hierarchy(klass)
  ptr = klass
  chain = []
  loop do
    chain.push(*class_with_modules(ptr))
    break if !ptr.respond_to?(:superclass) || ptr.superclass.nil?
    ptr = ptr.superclass
  end
  chain
end
class_with_modules(klass) click to toggle source
# File lib/ama-entity-mapper/type/registry.rb, line 105
def class_with_modules(klass)
  if klass.superclass.nil?
    parent_modules = []
  else
    parent_modules = klass.superclass.included_modules
  end
  [klass, *(klass.included_modules - parent_modules)]
end