Code Monkey home page Code Monkey logo

writ's Introduction

Writ: A minimal command pattern implementation

Build Status RubyGem

writ, noun:
         a form of written command in the name of a court or other legal authority to act, or abstain from acting, in some way.

Writ is a minimal command pattern implementation, including input validation, built on top of Scrivener.

Example

# Define your commands
class LogIn < Writ
  attr_accessor :email
  attr_accessor :password

  def validate
    assert_email :email
    assert_present :password
  end

  def run
    user = User.authenticate(email, password)
    assert user, [:email, :not_valid]
  end
end

# And use them
outcome = LogIn.run(req.params)

if outcome.success?
  session[:user_id] = outcome.value.id
else
  outcome.input.password = nil # Don't leak it when rendering the form again
  render "auth/sign_in", errors: outcome.errors, form: outcome.input
end

Install

gem install writ

Validations

As with scrivener, you should define a validate method that defines all validations for the user input. You're also welcome to use validations inside of run, when an error can come from the process itself. The example above, where the email/password combination yields no user, is a good example. Another example would be having to make an API call that returns an error.

Take a look at the default validations that come from scrivener, or feel free to define custom ones to suit your domain.

Why

When using scrivener, I tend to add a run method to the objects to alter state based on user input alongside the validations. Some people like calling these objects "services", "use cases", "commands", or "interactions".

I find this useful to decouple complex actions from the actors involved in them, and to keep models "thin" by only caring about expressing the domain without burdening themselves with expressing how users can interact with the domain, or with concepts like validation and authorization.

After using this pattern in several production projects, I figured it might as well live in a gem I can reuse instead of copy-pasting code around.

License

This project is shared under the MIT license. See the attached LICENSE file for details.

writ's People

Contributors

foca avatar

Stargazers

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

Watchers

 avatar  avatar

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.