<commit_before># Solid Use Case

**Solid Use Case** is a gem to help you implement well-tested and flexible use cases. Solid Use Case is not a framework - it's a **design pattern library**. This means it works *with* your app's workflow, not against it.

## Installation

Add this line to your application's Gemfile:

    gem 'solid_use_case', '~> 1.0.2'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install solid_use_case

## Usage

At its core, this library is a light wrapper around [Deterministic](https://github.com/pzol/deterministic), a practical abstraction over the Either monad. Don't let that scare you - you don't have to understand monad theory to reap its benefits.

The only required method is the `#run` method.

### Rails Example

```ruby
class UserSignup < SolidUseCase::Command
  def run(params)
    attempt_all do
      step { validate(params) }
      step {|params| save_user(params) }
      try {|params| email_user(params) }
    end
  end

  def validate(params)
    user = User.new(params[:user])
    if user.valid?
      fail :invalid_user, :user => user
    else
      params[:user] = user
      Success(params)
    end
  end

  def save_user(params)
    user = params[:user]
    if user.save
      fail :user_save_failed, :user => user
    else
      Success(params)
    end
  end

  def email_user(params)
    UserMailer.async.deliver(:welcome, params[:user].id)
    Success(params[:user])
  end
end
```

Now you can run your use case in your controller and easily respond to the different outcomes (with **pattern matching**!):

```ruby
class UsersController < ApplicationController
  def create
    UserSignup.run(params).match do
      success do |user|
        flash[:success] = "Thanks for signing up!"
        redirect_to profile_path(user)
      end

      failure(:invalid_user) do |error_data|
        render_new(error_data, "Oops, fix your mistakes and try again")
      end

      failure(:user_save_failed) do |error_data|<commit_msg>Quick updates<commit_after># Solid Use Case

**Solid Use Case** is a gem to help you implement well-tested and flexible use cases. Solid Use Case is not a framework - it's a **design pattern library**. This means it works *with* your app's workflow, not against it.

## Installation

Add this line to your application's Gemfile:

    gem 'solid_use_case', '~> 1.0.2'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install solid_use_case

## Usage

At its core, this library is a light wrapper around [Deterministic](https://github.com/pzol/deterministic), a practical abstraction over the Either monad. Don't let that scare you - you don't have to understand monad theory to reap its benefits.

The only required method is the `#run` method.

### Rails Example

```ruby
class UserSignup < SolidUseCase::Command
  def run(params)
    attempt_all do
      step { validate(params) }
      step {|params| save_user(params) }
      try {|params| email_user(params) }
    end
  end

  def validate(params)
    user = User.new(params[:user])
    if !user.valid?
      fail :invalid_user, :user => user
    else
      params[:user] = user
      next_step(params)
    end
  end

  def save_user(params)
    user = params[:user]
    if user.save
      fail :user_save_failed, :user => user
    else
      next_step(params)
    end
  end

  def email_user(params)
    UserMailer.async.deliver(:welcome, params[:user].id)
    return params[:user]
  end
end
```

Now you can run your use case in your controller and easily respond to the different outcomes (with **pattern matching**!):

```ruby
class UsersController < ApplicationController
  def create
    UserSignup.run(params).match do
      success do |user|
        flash[:success] = "Thanks for signing up!"
        redirect_to profile_path(user)
      end

      failure(:invalid_user) do |error_data|
        render_new(error_data, "Oops, fix your mistakes and try again")
      end

      failure(:user_save_failed) do |error_data|
{'fraction_non_alphanumeric': 0.09363295880149813, 'fraction_numerical': 0.00149812734082397, 'mean_word_length': 3.2753468516542155, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6377364', 'n_tokens_mistral': 1242, 'n_tokens_neox': 1174, 'n_words': 439}