-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.rb
executable file
·38 lines (33 loc) · 1.44 KB
/
source.rb
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
require_relative 'configurable'
require_relative 'source/normal_source.rb'
module Shark
# This module provides a default Source class that all Sources should inherit
# from, as well as utilities to register and instantiate new source types
# based on humanized names.
module Source
extend self
include Configurable
# Register a new Source class under a humanized name. Source classes will
# be unique for a given name-object_type pair. That is, multiple Sources
# can share the same humanized name, but have unique
def register_source name, object_type, klass, fail_on_override: true
@@sources ||= {}
key = [name, object_type]
# Unless supressed, raise an error if a Source is already registered
# under the given name-object_type pair.
if @@sources[key] and fail_on_override
raise "Source #{key} already exists. Use a different name or supress with `fail_on_override: false`."
else
@@sources[key] = klass
end
end
# Instantiate a new Source class, the exact type of which is determined by
# set of currently registered sources and the name-object_type pair given.
# `config` will be passed through to the initializer for the new instance.
def create name, object_type, config={}
klass = @@sources[[name, object_type]]
raise "No Source registered with name #{name} and type #{object_type}." unless klass
klass.new(config)
end
end
end