Skip to content

TypedOperation A command pattern for Ruby with typed parameters

Partial application, currying, and operation composition — with runtime type safety and first-class Rails, Dry::Monads, and Action Policy integration.

Type-safe parameters

Declare an operation’s inputs with type constraints, validated at runtime via the Literal gem. A wrong-shape argument fails loudly the moment the operation is built.

Partial application & currying

Build specialized operations from general ones. Fix some parameters with .with, leave the rest open, or .curry to apply them one at a time.

Operation composition

Chain operations with .then and .or_else for railway-oriented flows, or compose them declaratively with the Pipeline DSL.

Result types

Built-in Success/Failure types with no dependencies, or drop in Dry::Monads for Result types and Do notation when you want them.

Rails integration

Generators scaffold your base class and operations. bin/rails g typed_operation:install wires everything up in one step.

Action Policy authorization

Declare authorization inline with action_type and authorized_via. Operations check the policy before perform runs.

What is TypedOperation?

TypedOperation is an implementation of the Command pattern: each operation is a small, callable object with explicitly typed parameters. Operations can be partially applied, curried, pattern-matched, and composed into larger flows — so business logic lives in consistent, testable, isolated units.

class GreetUser < TypedOperation::Base
  param :name, String
  param :greeting, String, default: "Hello"

  def perform
    "#{greeting}, #{name}!"
  end
end

# Direct invocation
GreetUser.call(name: "World")
# => "Hello, World!"

# Partial application
greeter = GreetUser.with(greeting: "Welcome")
greeter.call(name: "Alice")
# => "Welcome, Alice!"

Installation

Add this line to your application’s Gemfile:

gem "typed_operation"

For Rails, generate the base operation class:

bin/rails g typed_operation:install

# With optional integrations
bin/rails g typed_operation:install --dry_monads --action_policy

Where to next