Module: Injectable

Defined in:
lib/injectable.rb,
lib/injectable/version.rb,
lib/injectable/dependency.rb,
lib/injectable/class_methods.rb,
lib/injectable/instance_methods.rb,
lib/injectable/dependencies_graph.rb,
lib/injectable/missing_dependencies_exception.rb,
lib/injectable/method_already_exists_exception.rb

Overview

Convert your class into an injectable service

Examples:

You would create a service like this:

class AddPlayerToTeamRoster
  include Injectable

  dependency :team_query
  dependency :player_query, class: UserQuery

  argument :team_id
  argument :player_id

  def call
    player_must_exist!
    team_must_exist!
    team_must_accept_players!

    team.add_to_roster(player)
  end

  private

  def player
    @player ||= player_query.call(player_id)
  end

  def team
    @team ||= team_query.call(team_id)
  end

  def player_must_exist!
    player.present? || raise UserNotFoundException
  end

  def team_must_exist!
    team.present? || raise TeamNotFoundException
  end

  def team_must_accept_players!
    team.accepts_players? || raise TeamFullException
  end
end

And use it like this:

AddPlayerToTeamRoster.call(player_id: player.id, team_id: team.id)

Defined Under Namespace

Modules: ClassMethods, InstanceMethods Classes: DependenciesGraph, Dependency, MethodAlreadyExistsException, MissingDependenciesException

Constant Summary collapse

VERSION =
'2.1.1'.freeze

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



58
59
60
61
# File 'lib/injectable.rb', line 58

def self.included(base)
  base.extend(Injectable::ClassMethods)
  base.prepend(Injectable::InstanceMethods)
end