Sha256: 937d68ed291fb4af0ba2e336c7ee15b10a20b5ecdf2dd316ea02523d4433c660
Contents?: true
Size: 1.32 KB
Versions: 22
Compression:
Stored size: 1.32 KB
Contents
module Riveter module AssociatedTypeRegistry extend ActiveSupport::Concern included do container = self type_registry = TypeRegistry.new() define_singleton_method :container do container end define_singleton_method :registry do type_registry end end module ClassMethods def key_for(type) type.is_a?(Symbol) ? type : type.name.underscore.to_sym end def register_type(type, associated_type, options={}) options = { :key => key_for(associated_type) }.merge(options) # prevent duplicate registrations type_list = container.registry[options[:key]] type_list << type unless type_list.include?(type) end def resolve!(type) registered = self.resolve(type) raise UnregisteredTypeError.new(type) unless registered registered end def resolve(type) self.resolve_all(type).first end def resolve_all(type) container.registry[key_for(type)] || [] end end end class TypeRegistry < Hash def initialize super {|h, k| h[k] = []} end end class UnregisteredTypeError < StandardError attr_reader :type def initialize(type) super "Unregistered type '#{type}'" @type = type end end end
Version data entries
22 entries across 22 versions & 1 rubygems