Rohbau

Travis Gem Version Code Climate Test Coverage Inline docs

Gem | Source | Documentation

Description

Rohbau provides a set of patterns used in Domain Driven Design.

Installation

Add this line to your application's Gemfile:

gem 'rohbau'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rohbau

Usage

Runtime

By instantiation of the RuntimeLoader, an instance of the Runtimeis created and stored as a singleton. Internal units of the respective component can access this instance by referring to the RuntimeLoader. By this a place is made where for example memories for in-memory gateway backend implementations can be stored.

Examples

Inject a user service to your application

require 'rohbau/runtime'
require 'rohbau/runtime_loader'

module MyApplication
  class RuntimeLoader < Rohbau::RuntimeLoader
    def initialize
      super(Runtime)
    end
  end

  class Runtime < Rohbau::Runtime
  end
end

module UserService
  class RuntimeLoader < Rohbau::RuntimeLoader
    def initialize
      super(Runtime)
    end
  end

  class Runtime < Rohbau::Runtime
  end
end

# Register user service on my application runtime
MyApplication::Runtime.register :user_service, UserService::RuntimeLoader
MyApplication::Runtime.plugins # => {:user_service=>UserService::RuntimeLoader}

# Runtimes are not initialized yet
MyApplication::RuntimeLoader.instance # => nil
MyApplication::Runtime.plugins[:user_service].instance # => nil

# Boot my application runtime
MyApplication::RuntimeLoader.running? # => false
MyApplication::RuntimeLoader.new
MyApplication::RuntimeLoader.running? # => true

# Runtimes are initialized
MyApplication::RuntimeLoader.instance # => #<MyApplication::Runtime:0x00000000f5b8d8 @user_service=UserService::RuntimeLoader>
MyApplication::Runtime.plugins[:user_service].instance # => #<UserService::Runtime:0x00000000b1ecc0>

# Runtimes are singletons
MyApplication::RuntimeLoader.instance === MyApplication::RuntimeLoader.instance
MyApplication::Runtime.plugins[:user_service].instance === MyApplication::Runtime.plugins[:user_service].instance

# Terminate my application runtime
MyApplication::RuntimeLoader.terminate
MyApplication::RuntimeLoader.running? # => false

Registrar

Every injected RuntimeLoader knows about it's registrar. In the example above UserService::RuntimeLoader has been injected to MyApplication::RuntimeLoader. UserService::RuntimeLoader.registrar therefore returns MyApplication::RuntimeLoader.

List of plugins

Accordingly to the sample above MyApplication::RuntimeLoader knows about it's registered plugins. MyApplication::RuntimeLoader.plugins therefore returns {:user_service => UserService::RuntimeLoader}.

ServiceFactory

The ServiceFactory is considered the authority for retrieval of service instances. It follows partly the service locator / registry pattern.

Examples

Register and unregister default service and override with specific service.

require 'rohbau/service_factory'

MyServiceFactory = Class.new(Rohbau::ServiceFactory)

user_service_1 = Struct.new(:users).new([:alice, :bob])
user_service_2 = Struct.new(:users).new([:jim, :kate])

runtime = Object.new
registry = MyServiceFactory.new(runtime)

MyServiceFactory.register(:user_service) { user_service_1 }
registry.user_service.users # => [:alice, :bob]

MyServiceFactory.register(:user_service) { user_service_2 }
registry.user_service.users # => [:jim, :kate]

MyServiceFactory.unregister(:user_service)
registry.user_service.users # => [:alice, :bob]

MyServiceFactory.unregister(:user_service)
registry.user_service # => NoMethodError: undefined method `user_service'

Validate registered dependencies

MyServiceFactory.external_dependencies :user_service
MyServiceFactory.missing_dependencies # => [:user_service] 
MyServiceFactory.external_dependencies_complied? # => false

MyServiceFactory.register(:user_service) { Object.new } # => :user_service 
MyServiceFactory.external_dependencies_complied? # => true 
MyServiceFactory.missing_dependencies # => [] 

Build README

Make changes to README.md.template, not to README.md

Include examples with

include_example example_file_name

Build README.md with

  ./bin/build_readme

Always commit README.md.template and README.md together.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request