• Stars
    star
    43
  • Rank 645,449 (Top 13 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 5 years ago
  • Updated 9 months ago

Reviews

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

Repository Details

Objects are functions! Treat any Object or Class as a Proc (like Enumerable but for Procs).

Ruby Gem Version Contact me on Codementor

Buy Me A Coffee

Invokable

Objects are functions! Treat any Object or Class as a Proc (like Enumerable but for Procs)

Synopsis

Objects that enclose state, can be treated as automatically curried functions.

require 'invokable'

class TwitterPoster
  include Invokable

  def initialize(model)
    @model = model
  end

  def call(user)
    # do the dirt
    ...
    TwitterStatus.new(user, data)
  end
end

TwitterPoster.call(Model.find(1)) # => #<TwitterPoster ...>
TwitterPoster.call(Model.find(1), current_user) # => #<TwitterStatus ...>

# both the class and it's instances can be used anywhere Procs are.

Model.where(created_at: Date.today).map(&TwitterPoster) # => [#<TwitterPoster ...>, ...]

Use memoize, << and >> for composition and other methods on Proc on your command objects

# app/queries/filter_records.rb
class FilterRecords
  include Invokable

  def initialize(params)
    @params = params
  end

  def call(records)
    # do filtering return records
  end
end

# app/queries/sort_records.rb
class SortRecords
  include Invokable

  def initialize(params)
    @params = params
  end

  def call(records)
    # do sorting return records
  end
end

sort_and_filter = SortRecords.call(params) << FilterRecords.call(params)
sort_and_filter.call(records) # => sorted and filtered records

Helper methods that can be used with any object that responds to call or to_proc

Invokable.juxtapose(:sum, -> (xs) { xs.reduce(:*) }, :min, :max).([3, 4, 6]) # => [13, 72, 3, 6]

Invokable.knit(:upcase, :downcase).(['FoO', 'BaR']) # => ["FOO", "bar"]

They are also mixed into any class that includes the module

class Transformer
  include Invokable

  def call(array)
    array.map(&juxtapose(identity, compose(:to_s, :upcase))).to_h
  end
end

Transformer.call([:a, :b, :c, :d]) # => {:a => "A", :b => "B", :c => "C", :d => "D"} 

Hashes can be treated as functions of their keys

require 'invokable'
require 'invokable/hash'

number_names = { 1 => "One", 2 => "Two", 3 => "Three" }
[1, 2, 3, 4].map(&number_names) # => ["One", "Two", "Three", nil]

Arrays can be treated as functions of their indexes

require 'invokable'
require 'invokable/array'

alpha = ('a'..'z').to_a
[1, 2, 3, 4].map(&alpha) # => ["b", "c", "d", "e"]

Sets can be treated as predicates

require 'invokable'
require 'invokable/set'

favorite_numbers = Set[3, Math::PI]
[1, 2, 3, 4].select(&favorite_numbers) # => [3]

Use as much or a little as you need

require 'invokable'         # loads Invokable module
require 'invokable/helpers' # loads Invokable::Helpers module
require 'invokable/hash'    # loads hash patch
require 'invokable/array'   # loads array patch
require 'invokable/set'     # loads set patch
require 'invokable/data'    # loads hash, set and array patches

Why?

A function is a mapping of one value to another with the additional constraint that for the one input value you will always get the same output value. So, conceptually, Ruby Hashes, Arrays, and Sets are all functions. Also, there are many one method objects out there (e.g. Service Objects) that are essentially functions. Why not treat them as such?

Installation

Add this line to your application's Gemfile:

gem 'invokable'

And then execute:

> bundle

Or install it yourself as:

> gem install invokable

API Documentation

https://www.rubydoc.info/gems/invokable

See Also

Compatibility

Tested using MRI 2.4, 2.5, 2.6, 2.7, 3.0 and JRuby

License

The gem is available as open source under the terms of the MIT License.

More Repositories

1

mini-levenshtein

Simple, fast Levenshtein distance and similarity ratio for Ruby
C
27
star
2

activerecord-setops

Union, Intersect, and Difference set operations for ActiveRecord (also, SQL's UnionAll).
Ruby
21
star
3

magbot

A CLI application for fetching media from jw.org feeds
Perl
9
star
4

zera-5

A light-weight Clojure interpreter
JavaScript
8
star
5

activerecord-pull

A simple query interface for pulling deeply nested data from records.
Ruby
4
star
6

contracts-gen

Generate data from contracts
Ruby
2
star
7

dragnet

A work-in-progress system for programmable surveys, and forms.
Ruby
2
star
8

ALJAFP

A Little Java, A Few Patterns
Java
1
star
9

piglatin

Pig Latin translator
Perl
1
star
10

cantor

Relational Algebra and Reporting
Ruby
1
star
11

atomjs

Clojure Atoms for Javascript. Shared, synchronous, independent state. A fork of https://github.com/cjohansen/js-atom.
JavaScript
1
star
12

sinatra-rest-service-auth

Ruby
1
star
13

sleepr

Stop hacking and get some sleep
Perl
1
star
14

connect4

JavaScript
1
star
15

tether

Ruby
1
star
16

tetris.js

An (incomplete) tetris clone
JavaScript
1
star
17

wonderscript-alpha

A lisp for the web
TypeScript
1
star
18

periscope

A Perl Module for viewing sites through a periscope
Perl
1
star
19

pandora-periscope

A Periscope for Pandora
Perl
1
star
20

java-posix

POSIX for Java a clone (for posterity, I have no intention of maintaining this) imported from tarball found here: http://www.bmsi.com/java/posix/index.html
Java
1
star
21

Pudu

A light-weight Moose-like object system that makes it easy to create encapsulated, immutable objects
Perl
1
star
22

Makakilo-Elementary

Ruby
1
star
23

sleepr-preferences

Preferences dialog for sleepr
Python
1
star
24

yugo

An experimental tool for converting legacy ColdFusion applications into Ruby / Rack applications.
Ruby
1
star
25

talks

1
star
26

asdf-vm.el

asdf-vm integration for Emacs
Emacs Lisp
1
star
27

kigo

Ruby is already pretty lispy let's take it the rest of the way.
Ruby
1
star
28

rubygems

A fork to add offline installation features, all changes are in offline branch
Ruby
1
star