A simple gem for eliminating Ruby initializers boilerplate code, and providing unified service objects API
A simple gem for eliminating Ruby initializers boilerplate code, and providing unified service objects API
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
endThis 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.
In your Gemfile
gem 'smart_init'You can use it either by extending a module:
require 'smart_init'
class ApiClient
extend SmartInit
initialize_with :network_provider, :api_token
endor subclassing:
class ApiClient If you omit a required attribute an ArgumentError will be thrown:
client = ApiClient.new(network_provider: Faraday.new)
# ArgumentError (missing required attribute api_token)You can use the is_callable method:
class Calculator resultOptionally you can customize a callable method name:
class Routine 4
Adder.call(num_a: 2, num_b: 3) => 5Contrary 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 #)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 #
client.api_token => 'secret_token'class SemiPublicApiClient #
client.api_token => 'secret_token' => # NoMethodError (private method `api_token' called for #)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 #)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 #
client.network_provider = Typhoeus::Request.new(...) => #
client.api_token => 'secret_token'
client.api_token = 'new_token' => 'new_token'class SemiPublicApiClient #
client.network_provider = Typhoeus::Request.new(...) => #
client.api_token => # NoMethodError (private method `api_token' called for #)
client.api_token = 'new_token' => # NoMethodError (undefined method `api_token=' called for #)Finally, you can mix them together like this:
class PublicReadersSemiPublicAccessorsApiClient #
client.network_provider = Typhoeus::Request.new(...) => #
client.api_token => 'secret_token'
client.api_token = 'new_token' => # NoMethodError (undefined method `api_token=' called for #)
client.timeout_length => 100
client.timeout_length = 150 => # NoMethodError (undefined method `timeout_length=' called for #)class SemiPublicReadersSemiPublicAccessorsApiClient #
client.network_provider = Typhoeus::Request.new(...) => #
client.api_token => # NoMethodError (private method `api_token' called for #)
client.api_token = 'new_token' => # NoMethodError (undefined method `api_token=' called for #)
client.timeout_length => 100
client.timeout_length = 150 => # NoMethodError (undefined method `timeout_length=' called for #)No open issues yet, or sync has not completed.