Module: Appfuel::Initialize

Defined in:
lib/appfuel/initialize.rb,
lib/appfuel/initialize/initializer.rb

Defined Under Namespace

Classes: Initializer

Class Method Summary collapse

Class Method Details

.define(name, envs = [], app_name = nil, &block) ⇒ Object

Dsl used to add an initializer into to the application container. This will add an initializer into the default app unless another name is given.

Parameters:

  • name (String)

    name of the initializer

  • envs (String, Symbol, Array) (defaults to: [])

    A env,list of envs this can run in

  • app_name (String) (defaults to: nil)

    name of app for this initializer



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/appfuel/initialize.rb', line 14

def define(name, envs = [], app_name = nil, &block)
  if !name.is_a?(String) && !name.include?('.')
    fail "initializer name must be a string in the in the form of " +
          "([feature|global].initializer_name)"
  end
  top, name = name.split('.')
  top = "features.#{top}" unless top == 'global'
  namespace   = "#{top}.initializers.#{name}"
  container   = Appfuel.app_container(app_name)
  initializer = Initializer.new(name, envs, &block)
  container.register(namespace, initializer)

  initializer
end

.handle_configuration(container, params = {}) ⇒ Dry::Container

Populate configuration definition that is in the container and add its results to the container. It also adds the environment from the config to the container for easier access.

Parameters:

  • container (Dry::Container)
  • params (Hash) (defaults to: {})
  • overrides (Hash)

    a customizable set of options

  • env (Hash)

    a customizable set of options

Returns:

  • (Dry::Container)

    that was passed in



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/appfuel/initialize.rb', line 54

def handle_configuration(container, params = {})

  config = if params.key?(:config)
              params[:config]
           else
              definition = container['config_definition']
              system_env = params[:env] || ENV
              overrides  = params[:overrides]  || {}
              definition.populate(env: system_env, overrides: overrides)
           end


  env = if params.key?(:app_env)
          params[:app_env]
        else
          config.fetch(:env) { fail "key (:env) is missing from config" }
        end

  container.register(:config, config)
  container.register(:env, env)

  container
end

.handle_repository_mapping(container, params = {}) ⇒ Object



78
79
80
81
# File 'lib/appfuel/initialize.rb', line 78

def handle_repository_mapping(container, params = {})
  initializer = container[:repository_initializer]
  initializer.call(container)
end

.run(params = {}) ⇒ Dry::Container

This will initialize the app by handling configuration and running all the initilizers, which will result in an app container that has registered the config, env, and anything else the initializers decide to add.

Parameters:

  • params (Hash) (defaults to: {})
  • app_name (Hash)

    a customizable set of options

Returns:

  • (Dry::Container)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/appfuel/initialize.rb', line 91

def run(params = {})
  unless Appfuel.setup?
    fail "Appfuel can not be initialized unit it is setup. " +
      "Please use setup_appfuel(params) dsl in your root " +
      "before  bootstrapping"
  end

  app_name  = params.fetch(:app_name) { Appfuel.default_app_name }
  container = Appfuel.app_container(app_name)
  if container.key?(:initialized) && container[:initialized] == true
    return container
  end

  handle_configuration(container, params)
  handle_repository_mapping(container, params)

  Appfuel.run_initializers('global', container, params[:exclude] || [])

  container.register(:initialized, true)
  container
end

.runlist(name, list, app_name = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/appfuel/initialize.rb', line 29

def runlist(name, list, app_name = nil)
  namespace = "#{name}.initializers.run"
  unless name == 'global'
    namespace = "features.#{namespace}"
  end
  list = [list] unless list.is_a?(Array)
  fail "run list must be [String|Symbol|Array]" unless list.is_a?(Array)

  container = Appfuel.app_container(app_name)
  container.register(namespace, list)
  nil
end