File: registry.rb

package info (click to toggle)
ruby-open-graph-reader 0.7.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,988 kB
  • sloc: ruby: 1,525; xml: 22; makefile: 2
file content (64 lines) | stat: -rw-r--r-- 2,015 bytes parent folder | download | duplicates (4)
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