Skip to content

fdutey/shirinji-rails

Repository files navigation

Shirinji-Rails

Integration of Shirinji in rails

Installation

Add this line to your application's Gemfile:

gem 'shirinji-rails'

And then execute:

$ bundle

General usage

Add this file in config/dependencies.rb

Shirinji::Map.new do
  # declare your dependencies here
end

Dependency resolver can be accessed through

Rails.application.config.shirinji.resolver

I suggest to use a helper to make your life easier.

# lib/my_app.rb

module MyApp
  module_function
  
  def bean(name)
    ::Rails.application.config.shirinji.resolver.resolve(name)
  end
end

# moments later, in controllers / job / rake tasks
service = MyApp.bean(:user_sign_up_service)
service.call(user) 

Rails entities DSL

Since controllers and jobs (and maybe other rails entities) are not under Shirinji's control, they can not receive dependencies through their constructors.

One solution is to manually create a method for each dependency but it's painful and make the code harder to read.

Rails integration actually provides a simple DSL to express dependencies.

# application_controller.rb

class ApplicationController < ActionController::Base 
  include ShirinjiRails::ResolverBinding
end

# in any controller

class FooController < ApplicationController
  dependency :config
  # Alias
  dependency foo_create_service: :create_service
  
  def index
    config #=> `config` dependency
    create_service # => `foo_create_service` dependency 
  end
end

class FooJob < ActiveJob::Base
  include ShirinjiRails::ResolverBinding

  dependency :config
 
  def perform
    config #=> `config` dependency
  end
end

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/fdutey/shirinji-rails.