All Projects → foca → writ

foca / writ

Licence: MIT license
Minimal command pattern implementation on top of Scrivener

Programming Languages

ruby
36898 projects - #4 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to writ

csharp-design-patterns-for-humans
Design Patterns for Humans™ - An ultra-simplified explanation - C# Examples
Stars: ✭ 1,086 (+6687.5%)
Mutual labels:  command-pattern
csharp-design-patterns-for-humans-examples
Complete C# Examples Refereed in csharp-design-patterns-for-humans
Stars: ✭ 50 (+212.5%)
Mutual labels:  command-pattern
toro
Tree oriented routing
Stars: ✭ 116 (+625%)
Mutual labels:  lesscode
ohm-crystal
Ohm for Crystal
Stars: ✭ 69 (+331.25%)
Mutual labels:  lesscode
ensure
Tiny shell scripts to install things in your mac.
Stars: ✭ 27 (+68.75%)
Mutual labels:  lesscode
Unity Design Pattern
🍵 All Gang of Four Design Patterns written in Unity C# with many examples. And some Game Programming Patterns written in Unity C#. | 各种设计模式的Unity3D C#版本实现
Stars: ✭ 2,600 (+16150%)
Mutual labels:  command-pattern
Active interaction
💼 Manage application specific business logic.
Stars: ✭ 1,717 (+10631.25%)
Mutual labels:  command-pattern
AspNetCore.BookStore
ASP.NET Core application using Command Pattern and Repository Pattern
Stars: ✭ 121 (+656.25%)
Mutual labels:  command-pattern
OOP-Design-Patterns
MET CS665 - OOP Design Patterns Code Examples
Stars: ✭ 74 (+362.5%)
Mutual labels:  command-pattern
Plastic
This project provides encapsulation of things like Domain, Application Rules, Business Rules or Business Logic in Application.
Stars: ✭ 30 (+87.5%)
Mutual labels:  command-pattern
actions
Software without side-effects. Redo and Undo.
Stars: ✭ 23 (+43.75%)
Mutual labels:  command-pattern

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].