• Stars
    star
    179
  • Rank 214,039 (Top 5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple gem for eliminating Ruby initializers boilerplate code, and providing unified service objects API

Smart Init - Simple service objects in Ruby Gem Version CircleCI

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

More Repositories

1

rails-pg-extras

Rails PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.
Ruby
1,128
star
2

termit

Translations with speech synthesis in your terminal as a ruby gem
Ruby
507
star
3

ecto_psql_extras

Ecto PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.
Elixir
359
star
4

rails-brotli-cache

Drop-in enhancement for Rails cache, offering better performance and compression with Brotli algorithm
Ruby
250
star
5

normit

Translations with speech synthesis in your terminal as a node package
JavaScript
240
star
6

activerecord-analyze

Add EXPLAIN ANALYZE to Rails Active Record query objects
Ruby
217
star
7

ruby-pg-extras

Ruby PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.
Ruby
127
star
8

node-postgres-extras

NodeJS PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.
JavaScript
71
star
9

univ3-revm-arbitrage

Uniswap V3 MEV arbitrage calculations with REVM
Rust
50
star
10

pg-locks-monitor

A simple tool to observe PostgreSQL database locks in Rails apps.
Ruby
50
star
11

devloop

An automated test runner for Rails that instantly executes specs based on a recent git diff output.
Ruby
41
star
12

ecto_extras

Ecto helper functions.
Elixir
38
star
13

lazyme

A simple gem to help you optimize your shell workflow
Ruby
37
star
14

python-pg-extras

Python PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.
Python
34
star
15

Locker

Ethereum Smart Contracts for locking Ether, ERC20 and ERC721 tokens based on time and price conditions
TypeScript
31
star
16

WaitForIt

Events and time based iOS app scenarios made easy.
Swift
26
star
17

.dotfiles

My development environment settings.
Shell
13
star
18

haskell-pg-extras

Haskell PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.
Haskell
9
star
19

ruby-jemalloc-node-yarn

Docker image of Ruby with Jemalloc Node 16 LTS and Yarn
Dockerfile
9
star
20

railsSearchKit

This Chrome extension provides easy access to the search bars every Rails developer needs.
JavaScript
8
star
21

mev-gas-optimization

Optimize MEV Arbitrage Smart Contract with Yul and Huff
Rust
7
star
22

dont_you_count

Disable count queries for selected Active Admin tables.
Ruby
7
star
23

pi-hole-docker-compose

pi-hole-docker-compose
7
star
24

rust-pg-extras

Rust PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.
Rust
6
star
25

activerecord-implicit-order

Ruby
5
star
26

delegate_it

A drop in replacement for ActiveSupport delegate method in non Rails projects.
Ruby
4
star
27

active-admin-tips

Active Admin tips and performance optimizations in action
Ruby
4
star
28

Siorbackend

Ruby
1
star
29

FRP_introduction

Comparison between observer and reactive approach to login form validations.
JavaScript
1
star
30

focus.apki.io

Landing page for Focus app
HTML
1
star
31

abstract_base

Abstract Class pattern Ruby gem
Ruby
1
star