Module: Appfuel

Defined in:
lib/appfuel.rb,
lib/appfuel/config.rb,
lib/appfuel/errors.rb,
lib/appfuel/request.rb,
lib/appfuel/version.rb,
lib/appfuel/response.rb,
lib/appfuel/config/db.rb,
lib/appfuel/presenter.rb,
lib/appfuel/run_error.rb,
lib/appfuel/domain/dsl.rb,
lib/appfuel/initialize.rb,
lib/appfuel/predicates.rb,
lib/appfuel/validation.rb,
lib/appfuel/msg_request.rb,
lib/appfuel/root_module.rb,
lib/appfuel/handler/base.rb,
lib/appfuel/config/search.rb,
lib/appfuel/domain/entity.rb,
lib/appfuel/log_formatter.rb,
lib/appfuel/handler/action.rb,
lib/appfuel/presenter/base.rb,
lib/appfuel/cli_msg_request.rb,
lib/appfuel/config/populate.rb,
lib/appfuel/handler/command.rb,
lib/appfuel/application/root.rb,
lib/appfuel/response_handler.rb,
lib/appfuel/storage/db/mapper.rb,
lib/appfuel/storage/file/base.rb,
lib/appfuel/config/file_loader.rb,
lib/appfuel/handler/inject_dsl.rb,
lib/appfuel/storage/repository.rb,
lib/appfuel/domain/value_object.rb,
lib/appfuel/feature/initializer.rb,
lib/appfuel/testing_spec/helpers.rb,
lib/appfuel/validation/validator.rb,
lib/appfuel/config/definition_dsl.rb,
lib/appfuel/feature/action_loader.rb,
lib/appfuel/handler/validator_dsl.rb,
lib/appfuel/storage/db/repository.rb,
lib/appfuel/storage/memory/mapper.rb,
lib/appfuel/application/dispatcher.rb,
lib/appfuel/initialize/initializer.rb,
lib/appfuel/handler/handler_failure.rb,
lib/appfuel/storage/repository/base.rb,
lib/appfuel/storage/repository/expr.rb,
lib/appfuel/domain/entity_collection.rb,
lib/appfuel/storage/dynamodb/adapter.rb,
lib/appfuel/application/app_container.rb,
lib/appfuel/domain/domain_name_parser.rb,
lib/appfuel/storage/memory/repository.rb,
lib/appfuel/storage/repository/mapper.rb,
lib/appfuel/storage/repository/runner.rb,
lib/appfuel/validation/validator_pipe.rb,
lib/appfuel/application/feature_helper.rb,
lib/appfuel/storage/db/migration_tasks.rb,
lib/appfuel/storage/web_api/http_model.rb,
lib/appfuel/storage/web_api/repository.rb,
lib/appfuel/storage/db/migration_runner.rb,
lib/appfuel/storage/db/repository_query.rb,
lib/appfuel/storage/dynamodb/repository.rb,
lib/appfuel/storage/repository/criteria.rb,
lib/appfuel/storage/repository/settings.rb,
lib/appfuel/storage/dynamodb/primary_key.rb,
lib/appfuel/storage/repository/order_expr.rb,
lib/appfuel/storage/db/active_record_model.rb,
lib/appfuel/storage/elastic_search/adapter.rb,
lib/appfuel/storage/repository/expr_parser.rb,
lib/appfuel/storage/repository/initializer.rb,
lib/appfuel/storage/repository/mapping_dsl.rb,
lib/appfuel/storage/repository/storage_map.rb,
lib/appfuel/storage/db/migration_initializer.rb,
lib/appfuel/storage/repository/mapping_entry.rb,
lib/appfuel/storage/repository/search_parser.rb,
lib/appfuel/storage/elastic_search/repository.rb,
lib/appfuel/storage/repository/expr_transform.rb,
lib/appfuel/storage/repository/expr_conjunction.rb,
lib/appfuel/storage/repository/search_transform.rb,
lib/appfuel/storage/repository/mapping_collection.rb,
lib/appfuel/application/container_class_registration.rb

Defined Under Namespace

Modules: Application, Config, Db, Domain, Dynamodb, ElasticSearch, Feature, File, Handler, Initialize, Memory, Predicates, Presenter, Repository, RootModule, TestingSpec, Validation, WebApi Classes: CliMsgRequest, Errors, LogFormatter, MsgRequest, Request, Response, ResponseHandler, RunError

Constant Summary collapse

VERSION =
"0.7.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.framework_containerDry::Container

The framework dependency injection container holds information specific to appfuel plus an app container for each app it will manage. While it is most common to has only a single app it is designed to host multiple app since all there dependencies are contained in one container.

Returns:

  • (Dry::Container)


46
47
48
# File 'lib/appfuel.rb', line 46

def framework_container
  @framework_container ||= Dry::Container.new
end

Class Method Details

.app_container(name = nil) ⇒ Dry::Container

The application container is a di container used to hold all dependencies for the given application.

Parameters:

  • name (String, Nil) (defaults to: nil)

    default name is used when name is nil

Returns:

  • (Dry::Container)


80
81
82
# File 'lib/appfuel.rb', line 80

def app_container(name = nil)
  framework_container[name || default_app_name]
end

.default_app?Bool

The default app name must exist and the container must be registered for this to be true

Returns:

  • (Bool)


55
56
57
58
# File 'lib/appfuel.rb', line 55

def default_app?
  framework_container.key?(:default_app_name) &&
    framework_container.key?(framework_container[:default_app_name])
end

.default_app_nameString

Used when retrieving, resolving an item from or registering an item with an application container without using its name. The default app is considered the main app where all others have to be specified manually. This is assigned during setup via the module Appfuel::Initialize::Setup

Returns:

  • (String)


69
70
71
# File 'lib/appfuel.rb', line 69

def default_app_name
  framework_container[:default_app_name]
end

.expand_container_key(key, category) ⇒ Object



204
205
206
207
208
209
210
211
# File 'lib/appfuel.rb', line 204

def expand_container_key(key, category)
  parts = key.to_s.split('.')
  parts.insert(1, category)
  if parts.first != 'global'
    parts.unshift('features')
  end
  parts.join('.')
end

.presenter(key, opts = {}, &block) ⇒ Object

memberships.user => features.memberships.presenters.user global.user => global.presenters.user

array

global
user

array

membership
user


196
197
198
199
200
201
202
# File 'lib/appfuel.rb', line 196

def presenter(key, opts = {}, &block)
  key = expect_container_key(key, 'presenters')
  container = app_container(root_name | default_app_name)

  container.register(key, '')

end

.register(key, value, app_name = nil) ⇒ Object

Register an item in the application container

Parameters:

  • key (String)

    key of the item in the app container

  • app_name (String, Nil) (defaults to: nil)

    name of the app container

Returns:

  • the item that was resolved with name



110
111
112
113
114
115
116
# File 'lib/appfuel.rb', line 110

def register(key, value, app_name = nil)
  di = app_container(app_name)
  unless di.respond_to?(:register)
    fail "Application container (#{app_name}) does not implement :register"
  end
  di.register(key, value)
end

.resolve(key, app_name = nil) ⇒ Object

Resolve an item out of the application container

Parameters:

  • key (String)

    key of the item in the app container

  • app_name (String, Nil) (defaults to: nil)

    name of the app container

Returns:

  • the item that was resolved with name



93
94
95
96
97
98
99
# File 'lib/appfuel.rb', line 93

def resolve(key, app_name = nil)
  di = app_container(app_name)
  unless di.respond_to?(:resolve)
    fail "Application container (#{app_name}) does not implement :resolve"
  end
  di.resolve(key)
end

.run_initializers(key, container, exclude = []) ⇒ Dry::Container

Run all initializers registered in the app container

Parameters:

  • container (Dry::Container)

    application container

  • app_name (String)

    name of the app for errors

  • params (Hash)
  • excludes (Hash)

    a customizable set of options

Returns:

  • (Dry::Container)

    same container passed in



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/appfuel.rb', line 145

def run_initializers(key, container, exclude = [])
  unless exclude.is_a?(Array)
    fail ArgumentError, ":exclude must be an array"
  end

  exclude.map! {|item| item.to_s}
  namespace    = "#{key}.initializers"
  runlist_key  = "#{namespace}.run"
  env          = container['env']
  config       = container['config']

  runlist = []
  if container.key?(runlist_key)
    runlist = container[runlist_key]
  end

  unless runlist.respond_to?(:each)
    fail "[initialize] runlist (#{runlist_key}) must implement :each"
  end

  runlist.each do |initializer_key|
    initializer_key = "#{namespace}.#{initializer_key}"
    init = container[initializer_key]
    if !init.env_allowed?(env) || exclude.include?(init.name)
      next
    end

    begin
      init.call(config, container)
    rescue => e
      app_name = container[:app_name]
      msg = "[Appfuel:#{app_name}] Initialization FAILURE - " + e.message
      error = RuntimeError.new(msg)
      error.set_backtrace(e.backtrace)
      raise error
    end
  end

  container
end

.setup?Boolean

This will tell you if the application container has been setup and ready for bootstrapping

Returns:

  • (Boolean)


134
135
136
# File 'lib/appfuel.rb', line 134

def setup?
  framework_container.key?(:setup) && framework_container[:setup] ==  true
end

.setup_container_dependencies(namespace_key, container) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/appfuel.rb', line 119

def setup_container_dependencies(namespace_key, container)

  init_key = "#{namespace_key}.initializers"

  unless container.key?(init_key)
    container.register(init_key, ThreadSafe::Array.new)
  end

  container
end