Module: Appfuel::Application::Root

Includes:
Dispatcher
Defined in:
lib/appfuel/application/root.rb

Instance Method Summary collapse

Methods included from Dispatcher

#default_exception_handler, #dispatch, #handle_exception, #load_error_handler

Instance Method Details

#bootstrap(params = {}) ⇒ Object



135
136
137
# File 'lib/appfuel/application/root.rb', line 135

def bootstrap(params = {})
  Initialize.run(params)
end

#build_app_container(params, container = Dry::Container.new) ⇒ Dry::Container

Initializes the application container with:

Application Container

root: This is the root module that holds the namespaces for all
      features, actions, commands etc. This is required.

root_path: This is the root path of app where the source code
           lives. We use this to autoload this features. This
           is still under design so it might not stay.

config_definition: This is the definition object that we use to
                   build out the configuration hash. This is optional

initializers: This is an array that hold all the initializers to be
              run. This builder will handle creating the array. It is
              populated via appfuel dsl Appfuel::Initialize#define

global.validators: This is a hash that holds all global validators.
                   this builder will handle creating the hash. It is
                   populated via appfuel dsl
                   Appfuel::Validator#global_validator

global.domain_builders:
global.presenters

Parameters:

  • root (Module)

    the root module of the application

  • container (Dry::Container) (defaults to: Dry::Container.new)

    dependency injection container

Returns:

  • (Dry::Container)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/appfuel/application/root.rb', line 93

def build_app_container(params, container = Dry::Container.new)
  root = params.fetch(:root) {
    fail ArgumentError, "Root module (:root) is required"
  }

  root_path = params.fetch(:root_path) {
    fail ArgumentError, "Root path (:root_path) is required"
  }

  feature_initializer = params.fetch(:feature_initializer) {
    Feature::Initializer.new
  }

  action_loader = params.fetch(:action_loader) {
    Feature::ActionLoader.new
  }

  repo_initializer = params.fetch(:repository_initializer) {
    Repository::Initializer.new
  }

  mappings = Repository::MappingCollection.new
  root_name = root.to_s.underscore
  container.register(:root, root)
  container.register(:root_name, root_name)
  container.register(:root_path, root_path)
  container.register(:auto_register_classes, [])
  container.register(:repository_mappings, mappings)
  container.register(:repository_cache, {})
  container.register(:repository_initializer, repo_initializer)
  container.register(:features_path, "#{root_name}/features")
  container.register(:feature_initializer, feature_initializer)
  container.register(:action_loader, action_loader)
  if params.key?(:config_definition)
    container.register(:config_definition, params[:config_definition])
  end

  Appfuel.setup_container_dependencies('global', container)

  container
end

#call(route, inputs = {}) ⇒ Object



139
140
141
142
143
# File 'lib/appfuel/application/root.rb', line 139

def call(route, inputs = {})
  container = Appfuel.app_container
  request   = Request.new(route, inputs)
  dispatch(request, container)
end

#handle_after_setup(hook, container) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/appfuel/application/root.rb', line 38

def handle_after_setup(hook, container)
  unless hook.respond_to?(:call)
    fail ArgumentError, "After setup hook (:after_setup) must " +
      "implement :call, which takes the di container as its only arg"
  end
  hook.call(container)
end

#handle_app_name(params, app_container, framework_container) ⇒ String

The application name is determined by the root module. We use the lower case underscored version of the root module name

Parameters:

  • root (Module)

    The root module of the application

  • params (Hash)

    input params from setup

  • app_name (Hash)

    a customizable set of options

  • default_app (Hash)

    a customizable set of options

Returns:

  • (String)


55
56
57
58
59
60
61
62
63
# File 'lib/appfuel/application/root.rb', line 55

def handle_app_name(params, app_container, framework_container)
  app_name = app_container[:root].to_s.underscore

  if params[:default_app] == true || !Appfuel.default_app?
    framework_container.register(:default_app_name, app_name)
  end

  app_name
end

#setup_appfuel(params = {}) ⇒ Dry::Container

Initialize Appfuel by creating an application container for the app represented by the root module passed in. The app container is a dependency injection container that is used thought the app.

Parameters:

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

    a customizable set of options

  • app_name (Hash)

    a customizable set of options

Returns:

  • (Dry::Container)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/appfuel/application/root.rb', line 17

def setup_appfuel(params = {})
  app_container       = params[:app_container] || Dry::Container.new
  framework_container = Appfuel.framework_container

  app_container = build_app_container(params, app_container)
  app_name = handle_app_name(params, app_container, framework_container)

  app_container.register(:app_name, app_name)
  framework_container.register(app_name, app_container)

  initialize = params.fetch(:initialize) { [] }
  app_container.register('global.initializers.run', initialize)

  if params.key?(:on_after_setup)
    handle_after_setup(params[:on_after_setup], app_container)
  end

  framework_container.register(:setup, true)
  app_container
end