A simple gem for eliminating Ruby initializers boilerplate code, and providing unified service objects API
Clone or download
Latest commit e1f81b1 Jan 2, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib Enable customizing method name Jan 2, 2019
test Enable customizing method name Jan 2, 2019
.gitignore kind of works Sep 24, 2018
.travis.yml update ruby versions Jan 1, 2019
Gemfile initial commit May 4, 2017
LICENSE.txt initial commit May 4, 2017
README.md Readme fix Jan 2, 2019
Rakefile add take tasks May 4, 2017
smart_init.gemspec add dependency May 4, 2017

README.md

Smart Init Build Status Gem Version

Do you find yourself writing a lot of boilerplate code like that?

def initialize(network_provider, api_token)
  @network_provider = network_provider
  @api_token = api_token
end

def self.call(network_provider, api_token)
  new(network_provider, api_token).call
end

Gem provides a simple DSL for getting rid of it. It offers an alternative to using Struct.new which does not check for number of parameters provided in initializer, exposes getters and instantiates unecessary class instances.

Smart Init offers a unified API convention for stateless service objects, accepting values in initializer and exposing one public class method call which instantiates new objects and accepts arguments passed to initializer.

Check out this blog post for my reasoning behind this approach to service object pattern.

Installation

In your Gemfile

gem 'smart_init'

API

You can use it either by extending a module:

class ApiClient
  extend SmartInit

  initialize_with :network_provider, :api_token
end

or subclassing:

class ApiClient < SmartInit::Base
  initialize_with :network_provider, :api_token
end

Now you can just:

object = ApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
# <ApiClient:0x007fa16684ec20 @network_provider=Faraday<...>, @api_token="secret_token">

If you omit a required attribute an ArgumentError will be thrown:

object = ApiClient.new(network_provider: Faraday.new)

# ArgumentError (missing required attribute api_token)

You can also use is_callable method:

class Calculator < SmartInit::Base
  initialize_with :data
  is_callable

  def call
    ...
    result
  end
end

Calculator.call(data: data) => result

Optionally you can customize a callable method name:

class Routine < SmartInit::Base
  initialize_with :params
  is_callable method_name: :run!

  def run!
    ...
  end
end

Routine.run!(params: params)

Default arguments

You can use hash based, default argument values:

class Adder < SmartInit::Base
  initialize_with :num_a, num_b: 2
  is_callable

  def call
    num_a + num_b
  end
end

Adder.call(num_a: 2) => 4
Adder.call(num_a: 2, num_b: 3) => 5

Legacy Arguments API

Alternatively you can use a legacy API without hash arguments and default values:

class Calculator < SmartInit::Base
  initialize_with_args :data
  is_callable

  def call
    ...
    result
  end
end

Calculator.call(data) => result