Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

calls

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

calls

  • 0.0.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Calls

Version License Build Status

TL; DR

Instead of writing a method in Ruby, you might as well write an entire class for the task. That's called the method object pattern and helps to reduce complexity.

This gem helps you to do that, like so:

class SaySometing
  include Calls

  # Input
  option :text

  # Output
  def call
    puts text
  end
end

SaySometihng.call(text: 'Hi there!') # => 'Hi there!'

Installation

# Add this to your Gemfile
gem 'calls'

Usage

If you only have one mandatory, obvious argument, this is what your implementation most likely would look like:

class CalculateTax
  include Calls

  param :product

  def call
    product.price * 0.1
  end
end

bike = Bike.new(price: 50)
CalculateTax.call(bike) # => 5

If you prefer to use named keywords, use this instead:

class CalculateTax
  include Calls

  option :product

  def call
    product.price * 0.1
  end
end

bike = Bike.new(price: 50)
CalculateTax.call(product: bike) # => 5

You can also use both params and options. They are all mandatory.

class CalculateTax
  include Calls

  param :product
  option :dutyfree

  def call
    return 0 if dutyfree
    product.price * 0.1
  end
end

bike = Bike.new(price: 50)
CalculateTax.call(bike, dutyfree: true) # => 0

You can make options optional by defining a default value in a proc:

class CalculateTax
  include Calls

  param :product
  option :dutyfree, default: -> { false }

  def call
    return 0 if dutyfree
    product.price * 0.1
  end
end

bike = Bike.new(price: 50)
CalculateTax.call(bike) # => 5

That's it!

History

A minimal implementation of the method object pattern would probably look like the following. This is sometimes also referred to as "service class".

This is what deadlyicon/calls originally used (that's where the gem name comes from).

class SaySometing
  def self.call(*args, &block)
    new.call(*args, &block)
  end

  def call(text)
    puts text
  end
end

Basically everything passed to MyClass.call(...) would be passed on to MyClass.new.call(...).

Even better still, it should be passed on to MyClass.new(...).call so that your implementation becomes cleaner:

class SaySometing
  def self.call(*args, &block)
    new(*args, &block).call
  end

  def initialize(text:)
    @text = text
  end

  def call
    puts @text
  end
end

People implemented that, but in doing so reinvented the wheel. Because now you not only have the method object pattern (i.e. call), now you also have to deal with initialization (i.e. new).

That's where the popular dry-initializer gem comes in. It is a battle-tested way to initialize objects with mandatory and optional attributes.

The calls gem (you're looking at it right now), combines both the method object pattern and dry initialization. The team, who initially came up with using dry-initializer, published the initial code version under the name method_object.

Caveats

  • params cannot be optional (or have default values). This is because there can be several params in a row, which leads to confusion when they are optional.

License

MIT License, see LICENSE.md

FAQs

Package last updated on 06 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc