百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

smart_init

> 编程语言
开源

一个简单的插件,用于消除 Ruby 初始化程序的模板代码,并提供统一的服务对象 API

196 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

一个简单的插件,用于消除 Ruby 初始化程序的模板代码,并提供统一的服务对象 API

Smart Init - Simple service objects in Ruby

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

ruby
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

ruby
gem 'smart_init'

API

You can use it either by extending a module:

ruby
require 'smart_init'

class ApiClient
  extend SmartInit

  initialize_with :network_provider, :api_token
end

or subclassing:

ruby
class ApiClient 

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

ruby
client = ApiClient.new(network_provider: Faraday.new)

# ArgumentError (missing required attribute api_token)

Making the object callable

You can use the is_callable method:

ruby
class Calculator  result

Optionally you can customize a callable method name:

ruby
class Routine  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:

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

ruby
class PublicApiClient  #
client.api_token => 'secret_token'
ruby
class SemiPublicApiClient  #
client.api_token => 'secret_token' => # NoMethodError (private method `api_token' called for #)

Accessors access

Similarly, this is how it would look if you tried to use a writer method:

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

ruby
class PublicApiClient  #
client.network_provider = Typhoeus::Request.new(...) => #
client.api_token => 'secret_token'
client.api_token = 'new_token' => 'new_token'
ruby
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:

ruby
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 #)
ruby
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 #)

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言