Description
A simple gem for eliminating Ruby initializers boilerplate code, and providing unified service objects API. It has recently been updated to work seamlessly with Ruby 2.7
Smart Init - Simple service objects in Ruby alternatives and similar gems
Based on the "Abstraction" category.
Alternatively, view Smart Init - Simple service objects in Ruby alternatives based on common mentions on social networks and blogs.
-
Trailblazer
The advanced business logic framework for Ruby. -
Interactor
Interactor provides a common interface for performing complex user interactions. -
Cells
View components for Ruby and Rails. -
wisper
A micro library providing Ruby objects with Publish-Subscribe capabilities -
Responders
A set of Rails responders to dry up your application -
ActiveInteraction
:briefcase: Manage application specific business logic. -
Decent Exposure
A helper for creating declarative interfaces in controllers -
Mutations
Compose your business logic into commands that sanitize and validate input. -
Rails Event Store
A Ruby implementation of an Event Store based on Active Record -
dry-types
Flexible type system for Ruby with coercions and constraints -
Light Service
Series of Actions with an emphasis on simplicity. -
Amoeba
A ruby gem to allow the copying of ActiveRecord objects and their associated children, configurable with a DSL on the model -
SimpleCommand
A simple, standardized way to build and use Service Objects (aka Commands) in Ruby -
Rectify
Build maintainable Rails apps -
Sequent
CQRS & event sourcing framework for Ruby -
Waterfall
A slice of functional programming to chain ruby services and blocks, thus providing a new approach to flow control. Make them flow! -
u-service
Represent use cases in a simple and powerful way while writing modular, expressive and sequentially logical code. -
dry-transaction
Business transaction DSL -
Clowne
A flexible gem for cloning models -
Surrounded
Create encapsulated systems of objects and focus on their interactions -
PageletRails
Improve perceived performance of your rails application with minimum effort -
Setsy
Settings for classes backed by a database with defaults. -
SuperModule
SuperModule allows defining class methods and method invocations the same way a super class does without using def included(base). This also succeeds ActiveSupport::Concern by offering lighter syntax -
Rocketman
๐ Rocketman help build event-based/pub-sub code in Ruby -
skinny_controllers
A pattern for allowing for easier testing of large projects' business logic -
Pathway
Define your business logic in simple steps -
Strategic
Strategic - Painless Strategy Pattern in Ruby and Rails -
SolidService
A servcie pattern with a simple API -
Invokable
Objects are functions! Treat any Object or Class as a Proc (like Enumerable but for Procs). -
Lionshare
A Ruby interface to the Lionshare API (cryptocurrency prices) -
EasilyTypable
Ruby module that facilitates English-like type checking in an inheritance hierarchy via "type_name?" methods -
grpc_serializer
A simple library to encode nested hash to grpc object and vice versa -
simple_active_link_to
Simple rails view helper to manage "active" state of a link -
dry-rb
dry-rb is a collection of next-generation Ruby libraries, each intended to encapsulate a common task.
Access the most powerful time series database as a service
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of Smart Init - Simple service objects in Ruby or a related project?
Popular Comparisons
README
Smart Init - Simple service objects in Ruby

Do you find yourself writing a lot of boilerplate code like this?
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
This 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:
require 'smart_init'
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:
client = ApiClient.new(network_provider: Faraday.new)
# ArgumentError (missing required attribute api_token)
Making the object callable
You can use the 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
Readers access
Contrary to using Struct, by default the reader methods are not publicly exposed:
client = ApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
client.api_token => # NoMethodError (private method `api_token' called for #<ApiClient:0x000..>)
Optionally you can make all or subset of readers public using the public_readers
config option. It accepts true
or an array of method names as an argument.
class PublicApiClient < SmartInit::Base
initialize_with :network_provider, :api_token, public_readers: true
end
client = PublicApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
client.network_provider => #<Faraday::Connection:0x000...>
client.api_token => 'secret_token'
class SemiPublicApiClient < SmartInit::Base
initialize_with :network_provider, :api_token, public_readers: [:network_provider]
end
client = SemiPublicApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
client.network_provider => #<Faraday::Connection:0x000...>
client.api_token => 'secret_token' => # NoMethodError (private method `api_token' called for #<SemiPublicApiClient:0x000...>)
Accessors access
Similarly, this is how it would look if you tried to use a writer method:
client = ApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
client.api_token = 'new_token' => # NoMethodError (private method `api_token=' called for #<ApiClient:0x000..>)
Optionally you can make all or subset of accessors public using the public_accessors
config option. It accepts true
or an array of method names as an argument. This will provide both reader and writer methods publicly.
class PublicApiClient < SmartInit::Base
initialize_with :network_provider, :api_token, public_accessors: true
end
client = PublicApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
client.network_provider => #<Faraday::Connection:0x000...>
client.network_provider = Typhoeus::Request.new(...) => #<Typhoeus::Request:0x000...>
client.api_token => 'secret_token'
client.api_token = 'new_token' => 'new_token'
class SemiPublicApiClient < SmartInit::Base
initialize_with :network_provider, :api_token, public_accessors: [:network_provider]
end
client = SemiPublicApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
client.network_provider => #<Faraday::Connection:0x000...>
client.network_provider = Typhoeus::Request.new(...) => #<Typhoeus::Request:0x000...>
client.api_token => # NoMethodError (private method `api_token' called for #<SemiPublicApiClient:0x000...>)
client.api_token = 'new_token' => # NoMethodError (undefined method `api_token=' called for #<SemiPublicApiClient:0x000...>)
Finally, you can mix them together like this:
class PublicReadersSemiPublicAccessorsApiClient < SmartInit::Base
initialize_with :network_provider, :api_token, :timeout,
public_readers: true, public_accessors: [:network_provider]
end
client = PublicReadersSemiPublicAccessorsApiClient.new(
network_provider: Faraday.new, api_token: 'secret_token', timeout_length: 100
)
client.network_provider => #<Faraday::Connection:0x000...>
client.network_provider = Typhoeus::Request.new(...) => #<Typhoeus::Request:0x000...>
client.api_token => 'secret_token'
client.api_token = 'new_token' => # NoMethodError (undefined method `api_token=' called for #<SemiPublicApiClient:0x000...>)
client.timeout_length => 100
client.timeout_length = 150 => # NoMethodError (undefined method `timeout_length=' called for #<SemiPublicApiClient:0x000...>)
class SemiPublicReadersSemiPublicAccessorsApiClient < SmartInit::Base
initialize_with :network_provider, :api_token, :timeout,
public_readers: [:timeout], public_accessors: [:network_provider]
end
client = SemiPublicReadersSemiPublicAccessorsApiClient.new(
network_provider: Faraday.new, api_token: 'secret_token', timeout_length: 100
)
client.network_provider => #<Faraday::Connection:0x000...>
client.network_provider = Typhoeus::Request.new(...) => #<Typhoeus::Request:0x000...>
client.api_token => # NoMethodError (private method `api_token' called for #<SemiPublicReadersSemiPublicAccessorsApiClient:0x000...>)
client.api_token = 'new_token' => # NoMethodError (undefined method `api_token=' called for #<SemiPublicReadersSemiPublicAccessorsApiClient:0x000...>)
client.timeout_length => 100
client.timeout_length = 150 => # NoMethodError (undefined method `timeout_length=' called for #<SemiPublicReadersSemiPublicAccessorsApiClient:0x000...>)