Giter Site home page Giter Site logo

solid-process / solid-result Goto Github PK

View Code? Open in Web Editor NEW
46.0 5.0 4.0 673 KB

Unleash a pragmatic and observable use of Result Pattern and Railway-Oriented Programming in Ruby.

Home Page: https://rubygems.org/gems/solid-result

License: MIT License

Ruby 99.98% Shell 0.02%
railway-oriented-programming ruby pattern-matching result-monad result-pattern rop rubygem solid-process

solid-result's Introduction

⚛️ Solid::Process

Write business logic for Ruby/Rails that scales.

Ruby Rails

Supported Ruby and Rails

This library is tested against:

Ruby / Rails 6.0 6.1 7.0 7.1 Edge
2.7
3.0
3.1
3.2
3.3
Head

Introduction

solid-process is a Ruby/Rails library designed to encapsulate business logic into manageable processes. It simplifies writing, testing, maintaining, and evolving your code, ensuring it remains clear and approachable as your application scales.

Key Objectives:

  1. Seamless Rails integration: Designed to fully complement the Ruby on Rails framework, this library integrates smoothly without conflicting with existing Rails conventions and capabilities.

  2. Support progressive mastery: Offers an intuitive entry point for novices while providing robust, advanced features that cater to experienced developers.

  3. Promote conceptual integrity and rapid onboarding: By maintaining a consistent design philosophy, solid-process reduces the learning curve for new developers, allowing them to contribute more effectively and quickly.

  4. Minimize technical debt: Facilitate smoother transitions and updates as your application expands and your team size increases.

  5. Enhanced observability: Equipped with sophisticated instrumentation mechanisms, the library enables detailed logging and tracing without compromising clarity, even when processes are nested. This ensures the code is both easy to understand and to observe.

Examples

Checkout the solid-rails-app for a full example of how to use solid-process in a Rails application. Or take a look at the examples folder in this repository.

Installation

Add this line to your application's Gemfile:

gem 'solid-process'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install solid-process

Usage

TODO: Write usage instructions here

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bundle exec rake dev to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/solid-process/solid-process. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

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

Code of Conduct

Everyone interacting in the Solid::Process project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

solid-result's People

Contributors

henriquebcustodio avatar mrbongiolo avatar serradura avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

solid-result's Issues

`Success` should halt the step chain when using `Continue` addon

Currently, Continue is an alias for Success(:continued, value), providing a kind of better readability when chaining steps together. One "problem" is that Success can still be used instead of Continue to advance the operation from one step to another. Thus making the addon contract "weak".

See the example below:

module Divide
  extend self, BCDD::Result.mixin(with: :Continue)

  def call(arg1, arg2)
    validate_numbers(arg1, arg2)
      .and_then(:validate_non_zero)
      .and_then(:divide)
  end

  private

  def validate_numbers(arg1, arg2)
    arg1.is_a?(::Numeric) or return Failure(:invalid_arg, 'arg1 must be numeric')
    arg2.is_a?(::Numeric) or return Failure(:invalid_arg, 'arg2 must be numeric')

    Success(:ok, [arg1, arg2]) # It's not clear that the other steps will still be processed
  end

  def validate_non_zero(numbers)
    return Continue(numbers) unless numbers.last.zero? # same here

    Failure(:division_by_zero, 'arg2 must not be zero')
  end

  def divide((number1, number2))
    Success(:division_completed, number1 / number2)
  end
end

This makes it harder to really differentiate Continue from Success.

So, a better approach should enforce that Success also halts the chain when used on any given step (just like Failure does), thus making Continue required on steps that still need further processing. See the example below:

module Divide
  extend self, BCDD::Result.mixin(with: :Continue)

  def call(arg1, arg2)
    validate_numbers(arg1, arg2)
      .and_then(:validate_dividing_by_one)
      .and_then(:validate_non_zero)
      .and_then(:divide)
  end

  private

  def validate_numbers(arg1, arg2)
    arg1.is_a?(::Numeric) or return Failure(:invalid_arg, 'arg1 must be numeric') # halt the processing and return a failed result
    arg2.is_a?(::Numeric) or return Failure(:invalid_arg, 'arg2 must be numeric') # halt the processing and return a failed result

    Continue([arg1, arg2]) # continue to the next step
  end

  def validate_dividing_by_one((number1, number2))
    return Success(:division_completed, number1) if number2 == 1 # halt the processing and return a successful result

    Continue([number1, number2]) # continue to the next step
  end

  def validate_non_zero(numbers)
    return Continue(numbers) unless numbers.last.zero? # continue to the next step

    Failure(:division_by_zero, 'arg2 must not be zero') # halt the processing and return a failed result
  end

  def divide((number1, number2))
    Success(:division_completed, number1 / number2) # halt the processing and return a failed result
  end
end

When Continue addon is DISABLED:

  1. Failure will halt the step chain;
  2. Success will continue the step chain.

When Continue addon is ENABLED:

  1. Failure will halt the step chain;
  2. Success will halt the step chain;
  3. Continue CANNOT be used to produce a "final" result (this should raise an error).

Enforcing those rules when using the ' Continue' addon would improve readability and "contract strictness" since it would be clear that any Failure and Success on a step would generate a final result, and any other steps are ignored.

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.