module Injectable

Convert your class into an injectable service

@example

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)

Constants

Dependency

Initialize a dependency based on the options or the block passed

VERSION

Public Class Methods

included(base) click to toggle source
# File lib/injectable.rb, line 59
def self.included(base)
  base.extend(Injectable::ClassMethods)
  base.prepend(Injectable::InstanceMethods)
end