A simple gem for eliminating Ruby initializers boilerplate code, and providing unified service objects API
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
test
.gitignore
.travis.yml
Gemfile
LICENSE.txt
README.md
Rakefile
smart_init.gemspec

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

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 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.

Installation

In your Gemfile

gem 'smart_init'

Keyword arguments API

You can use it either by extending a module:

class ApiClient
  extend SmartInit

  initialize_with_keywords :network_provider, :api_token
end

or subclassing:

class ApiClient < SmartInit::Base
  initialize_with_keywords :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">

You can also use is_callable method:

class Calculator < SmartInit::Base
  initialize_with_keywords :data
  is_callable

  def call
    ...
    result
  end
end

Calculator.call(data: data) => result

Default arguments (Beta)

You can use keyword based, default argument values:

class Adder < SmartInit::Base
  initialize_with_keywords :num_a, num_b: '2'
  is_callable

  def call
    num_a + num_b
  end
end

Adder.call(num_a: '2') => '22'
Adder.call(num_a: '2', num_b: '3') => '23'

Warning currently only string values work as default arguments. There's a failing spec describing this case.

There might be danger of code injection because it uses eval and string based class_eval behind the scenes. I added it as a proof of concept. PRs on how it could improved are welcome.

Legacy API

Alternatively you can use standard API without keyword arguments:

class Calculator < SmartInit::Base
  initialize_with :data
  is_callable

  def call
    ...
    result
  end
end

Calculator.call(data) => result

It does not support default argument values though.