Git Product home page Git Product logo

opie's Introduction

Opie

Build Status codecov Code Climate Gem Version

Opie gives you a simple API for creating Operations using the Railsway oriented programming paradigm.

API

The Opie::Operation API:

  • ::step(Symbol) -> void indicates a method that is executed in the operation sequence
  • #success? -> Boolean indicates whether the operation was successful
  • #failure? -> Boolean indicates whether the operation was a failure
  • #failure -> Opie::Failure | nil the failure if the operation is a failure?, nil when it's a success
  • #failures -> Array<Opie::Failure> | nil an array with all failures
  • #output -> * | nil the operation's last step return value, nil when the operation fails

Internal API:

  • #step_name(Any, Any?) -> Any the step signature. First argument is the input and the second argument is an optional context
  • #fail(error_type: Symbol, error_data: *) -> Opie::Failure used inside the steps to indicate that the operation has failed

Executing an operation:

input = { first_name: 'John', last_name: 'McClane', email: '[email protected]' }
context = { current_user: 'admin' }

CreateUserOperation.(input, context)

Tentative API

  • ::step(Array<Symbol>) -> void a series of methods to be called in parallel
  • ::step(Opie::Step) -> void an enforcer of a step signature which helps to compose other steps
  • ::failure(Symbol) -> void indicates a custom method name to handle failures

Usage

Simple Usage:

# Create an Operation for completing a Todo
class Todos::CompleteTodo < Opie::Operation
  step :find_todo
  step :mark_as_complete

  def find_todo(todo_id)
    todo = Todo.find_by(id: todo_id)
    return fail(:not_found, "Could not find the Todo using id: #{todo_id}") unless todo
    todo
  end

  def mark_as_complete(todo)
    success = todo.update(completed_at: Time.zone.now)
    return fail(:update) unless success
    todo
  end
end

class TodosController < ApplicationController
  def complete
    # invoke the operation
    result = Todos::CompleteTodo.(params[:id])
    if result.success? # if #success?
      render status: :created, json: result.output # use output
    else
      render status: :bad_request, json: { error: error_message(result.failure) } # otherwise use #failure
    end
  end

  private

  def error_message(failure)
    case failure.type
      when :not_found then failure.data
      when :update then 'We were unable to make the changes to your todo'
      else 'There was an unexpected error, sorry for the inconvenience'
    end
  end
end

Real world example:

Imagine yourself in the context of a habit tracker, wanting to add a new habit to track.

# app/controllers/habits_controller.rb

class HabitsController < ApplicationController
  # POST /habits
  def create
    # run the `operation` โ€“ since it's a modification we can call it a `command`
    result = People::AddHabit.(habit_params, operation_context)

    # render response based on operation result
    if result.success?
      render status: :created, json: result.output
    else
      render_operation_failure(result.failure)
    end
  end

  private

  def render_operation_failure(failure)
    render status: failure_http_status(failure.type), json: { errors: failure.data }
  end

  # the HTTP status depends on the error type, which separating the domain from the infrastructure
  def failure_http_status(type)
    case(type)
    when :unauthorized then :unauthorized
    when :validation then :unprocessable_entity 
    when :not_found then :not_found
    else :server_error
    end
  end

  # simulate parameters came from a Http request
  def habit_params
    {
      person_id: 2,
      name: 'Excercise',
      description: 'Did you excercise for at least 15 minutes today?',
      frequency: :three_times_per_week,
      color: 'DeepPink'
    }
  end

  def operation_context
    { current_user: current_user }
  end
end

And now the code that defines the operation

# application-wide dependencies container
class HabitTrackerContainer
  extends Dry::Container::Mixin

  register 'repositories.habit', HabitRepository.new
  register 'repositories.person', PersonRepository.new
  register 'service_bus', ServiceBus.new
end

# application-wide dependency injector
Import = Dry::AutoInject(HabitTrackerContainer.new)

module People
  # we define a validation schema for our input
  AddHabitSchema = Dry::Schema.Validation do
    configure do
      # custom predicate for frequency
      def freq?(value)
        [:weekly, :five_times_per_week, :four_times_per_week, :three_times_per_week].includes?(value)
      end
    end
    
    required(:person_id).filled(:int?, gt?: 0)
    required(:name).filled(:str?)
    required(:description).maybe(:str?)
    required(:frequency).filled(:freq?)
    required(:color).filled(:str?)
  end

  # the operation logic starts
  class AddHabit < Opie::Operation 
    # inject dependencies, more flexible than ruby's global namespace
    include Import[
      habit_repo: 'repositories.habit',
      person_repo: 'repositories.person',
      service_bus: 'service_bus'
    ]

    # first step receives ::call first argument, then the output of the step is the argument of the next step
    step :authorize
    step :validate
    step :find_person
    step :persist_habit
    step :send_event

    def authorize(params, context)
      # Authorize using Pundit's policy api
      return fail(:unauthorized) if HabitPolicy.new(context, Habit).add?
      params
    end

    # receives the first input
    def validate(params)
      schema = AddHabitSchema.(params)
      return fail(:validation, schema.errors) if schema.failure?
      schema.output
    end

    # if it's valid then find the person (tenant)
    def find_person(params)
      person = person_repo.find(params[:person_id])
      return fail(:repository, 'We could not find your account') unless person
      params.merge(person: person)
    end

    # persist the new habit
    def persist_habit(params)
      new_habit = Entities::Habit.new(params)
      habit_repo.create(new_habit)
    rescue => error
      fail(:persist_failed, error.message)
    end

    # notify the world
    def send_event(habit)
      event = Habits::CreatedEvent.new(habit.attributes)
      service_bus.send(event)
    rescue => error
      fail(:event_failed, error)
    end
  end
end

Installation

Add this line to your application's Gemfile:

gem 'opie'

And then execute:

$ bundle

Or install it yourself as:

$ gem install opie

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test 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 tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/guzart/opie.

License

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

opie's People

Contributors

guzart avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

opie's Issues

Support Async operations (Saga)

Use an adaptor to support job systems like Sidekiq, SuckerPunch, messaging systems like RabbitMQ, Amazon SMQ, and concurrency frameworks like Cellulloid

Improve API and example

Rules:

  • Each use case will be equivalent to a single function
  • The function will return a sum type with two cases: "Success" and "Failure"
  • The use case function will be built from a series of smaller functions each representing one step in a data flow
  • The errors from each step will be combined into a single "failure" path

Other functions:

  • Single track functions: success or failure track go right thru
  • Dead-end functions: Functions that don't return an output, like updating the DB. In this case the input is divided, one side goes thru and the other is a dead-end.
  • Functions that throw exceptions: IO, database, etc. Trap them and convert them into failures.
  • Supervisory functions (logging, monitoring): Similar to single track functions, success and failure go right thru with a watcher.
  • Compensating transactions (instead of two phase commit): Undo actions instead of wrapping everything in a transaction.

Terms:

  • Switch
  • Point
  • Track

Example

Habit builder

frequency: daily, five times per week, three times per week, two times per week, weekly
checkmark = checked, unchecked, checked implicitly
repetition = record user performed certain habit at a certain time
score = how strong a habit at a certain date is, max score, habit repeated 3 times in 8 days 3.0/8.0 = 0.375
streak = start, end
reminder = hour, minute, weekday list

Use dry-monad to support passing a block to the Operation::call

Humans::AddHabit.(params, repository: FakeUserRepository.new) do |m|
  m.success do |habit|
    render status: :created, json: habit
  end
  m.failure do |errors|
    render status: :unprocessable_entity, json: { errors: errors }
  end
end

def validate(params)
  schema = Schema.(params)
  if schema.valid?
    params
  else
    fail(schema.errors)
  end
end

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.