Class: Serf::Loader::Loader

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(*args) ⇒ Loader

Returns a new instance of Loader.



19
20
21
22
23
# File 'lib/serf/loader/loader.rb', line 19

def initialize(*args)
  opts = Optser.extract_options! args
  @registry_class = opts.get :registry_class, Serf::Loader::Registry
  @builder_class = opts.get :builder_class, Serf::Builder
end

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/'

Parameters:

  • opts (Hash)

    env and basepath options



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.extract_options! 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