lib/hanami/components.rb in hanami-1.0.0 vs lib/hanami/components.rb in hanami-1.1.0.beta1
- old
+ new
@@ -6,10 +6,33 @@
# Components are an internal Hanami that provides precise loading mechanism
# for a project. It is responsible to load frameworks, configurations, code, etc..
#
# The implementation is thread-safe
#
+ # @example
+ # Hanami::Components.resolved('repo') { UserRepository.new }
+ # Hanami::Components['repository.users'] # => #<UserRepository relations=[...]>
+ #
+ # Also you can use Hanami::Components with dry-auto_inject
+ #
+ # @example
+ # Hanami::Components.resolved('repo') { UserRepository.new }
+ # Hanami::Components['repository.users'] # => #<UserRepository relations=[...]>
+ #
+ # HanamiImport = Dry::AutoInject(Hanami::Components)
+ #
+ # class CreateUser
+ # include HanamiImport['repository.users']
+ #
+ # def call(payload)
+ # users.create(payload)
+ # end
+ # end
+ #
+ # CreateUser.new.call # => #<User:...>
+ # CreateUser.new(users: MockRepository.new).call # => #<MockUser:...>
+ #
# @since 0.9.0
# @api private
module Components
# Available components
#
@@ -85,12 +108,18 @@
component.call(Hanami.configuration)
end
end
end
- # Return the value of an already resolved component.
+ # Return the value of an already resolved component. Or raise error for not resolved component.
#
+ # @example
+ # Hanami::Components.resolved('repository.users') { UserRepository.new }
+ #
+ # Hanami::Components['repository.users'] # => #<UserRepository relations=[...]>
+ # Hanami::Components['repository.other'] # => error
+ #
# @param name [String] the component name
#
# @raise [ArgumentError] if the component is unknown or not resolved yet.
#
# @since 0.9.0
@@ -103,9 +132,18 @@
# Release all the resolved components.
# This is used for code reloading.
#
# NOTE: this MUST NOT be used unless you know what you're doing.
+ #
+ # @example
+ # Hanami::Components.resolved('repository.users') { UserRepository.new }
+ # Hanami::Components['repository.users'] # => #<UserRepository relations=[...]>
+ #
+ # Hanami::Components.release
+ # Hanami::Components['repository.users']
+ # # => ArgumentError: Component not resolved: `repo'.
+ # # => Resolved components are: ...
#
# @since 1.0.0
# @api private
def self.release
@_resolved.clear