Coder Social home page Coder Social logo

rakibulislam / smart_init Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pawurb/smart_init

0.0 3.0 0.0 33 KB

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

License: MIT License

Ruby 100.00%

smart_init's Introduction

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

def self.call(network_provider, api_token)
  new(network_provider, api_token).call
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 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:

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:

object = ApiClient.new(network_provider: Faraday.new)

# ArgumentError (missing required attribute api_token)

You can also use 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

Legacy Arguments API

Alternatively you can use a legacy API without hash arguments and default values:

class Calculator < SmartInit::Base
  initialize_with_args :data
  is_callable

  def call
    ...
    result
  end
end

Calculator.call(data) => result

smart_init's People

Contributors

pawurb avatar

Watchers

James Cloos avatar K M Rakibul Islam (Rakib) avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.