Class: Serf::Loader::Loader
- Inherits:
-
Object
- Object
- Serf::Loader::Loader
- Defined in:
- lib/serf/loader/loader.rb
Overview
The main loader that takes a serfup configuration file and instance evals all the registered components and serfs, which populates the serfup loader registry; then returns a mapping of serf kind names to the instantiated serfs.
Instance Method Summary collapse
-
#initialize(*args) ⇒ Loader
constructor
A new instance of Loader.
-
#serfup(*args) ⇒ Object
Loads up the components defined in a serfup configuration, wires up all the “exposed” serfs and returns them in a frozen map.
Constructor Details
Instance Method Details
#serfup(*args) ⇒ Object
Loads up the components defined in a serfup configuration, wires up all the “exposed” serfs and returns them in a frozen map.
Example Config:
# Config is a simple hash
config = Hashie::Mash.new
# List out the globbed filenames to load up.
config.globs = [
'example/**/*.serf'
]
# List out the parcel kinds that we need to have serfs built up
# and exposed in the returned Serf Map.
config.serfs = [
'subsystem/requests/create_widget'
]
Example Env Hash:
env = Hashie::Mash.new
env.web_service = 'http://example.com/'
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/serf/loader/loader.rb', line 55 def serfup(*args) opts = Optser. args globs = opts.get! :globs serfs = opts.get! :serfs base_path = opts.get :base_path, '.' env = opts.get(:env) { Hashie::Mash.new } @registry = @registry_class.new env: env # Load in all the components listed globs.each do |glob_pattern| globs = Dir.glob File.join(base_path, glob_pattern) globs.each do |filename| begin File.open filename do |file| contents = file.read instance_eval(contents) end rescue => e raise Serf::Errors::LoadFailure.new "File: #{filename}", e end end end # Construct all the "serfs" map = Hashie::Mash.new serfs.each do |serf| begin map[serf] = @registry[serf] rescue => e raise Serf::Errors::LoadFailure.new "Kind: #{serf}", e end raise "Missing Serf: #{serf}" if map[serf].nil? end # return a frozen registry, clear the registry @registry = nil map.freeze return map end |