1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
require "singleton"
require "forwardable"
require "set"
module OpenGraphReader
module Object
# Global registry of namespaces and their representing classes.
# Also tracks which verticals are defined.
#
# @api private
class Registry
extend Forwardable
include Singleton
class << self
extend Forwardable
# @!method register(namespace, klass)
# Register a new namespace in the registry.
#
# @param [String] namespace The namespace in colon separated form, for example <tt>og:image</tt>.
# @param [Class] klass The class to register. It should include {Object}.
# @api private
#
# @!method registered?(namespace)
# Check whether a namespace is registered.
#
# @param [String] namespace The namespace in colon separated form, for example <tt>og:image</tt>.
# @return [Bool]
# @api private
#
# @!method [](namespace)
# Fetch the class associated with the given namespace
#
# @param [String] namespace The namespace in colon separated form, for example <tt>og:image</tt>.
# @return [Class] The matching class.
# @raise [ArgumentError] If the given namespace wasn't registered.
# @api private
# @!method verticals
# All known verticals
#
# @return [Set<String>]
def_delegators :instance, :register, :registered?, :[], :verticals
end
def_delegators :@namespaces, :[]=, :has_key?
alias_method :register, :[]=
alias_method :registered?, :has_key?
# @see Registry.verticals
attr_reader :verticals
def initialize
@namespaces = {}
@verticals = Set.new
end
# @see Registry.[]
def [] namespace
raise UnknownNamespaceError, "#{namespace} is not a registered namespace" unless registered? namespace
@namespaces[namespace]
end
end
end
end
|