Giter Site home page Giter Site logo

imperator's Introduction

#Imperator Imperator is a small gem to help with command objects. The command pattern is a design pattern used to encapsulate all of the information needed to execute a method or process at a point in time. In a web application, commands are typically used to delay execution of a method from the request cycle to a background processor.

###Why use commands? The problem with controllers in Rails is that they're a part of the web domain—their job is to respond to requests, and ONLY to respond to requests. Anything that happens between the receipt of a request and sending a response is Somebody Else's Job™. Commands are that Somebody Else™. Commands are also very commonly utilized to put work into the background.

Why are commands an appropriate place to handle that logic? Commands give you the opportunity to encapsulate all of the logic required for an interaction in one spot. Sometimes that interaction is as simple as a method call—more often there are several method calls involved, not all of which deal with domain logic (and thus, are inappropriate for inclusion on the models). Commands give you a place to bring all of these together in one spot without increasing coupling in your controllers or models.

Commands can also be regarded as the contexts from DCI.

###Validation Commands also give you an appropriate place to handle interaction validation. Validation is most often regarded as a responsibility of the data model. This is a poor fit, because the idea of what's valid for data is very temporally tied to the understanding of the business domain at the time the data was created. Data that's valid today may well be invalid tomorrow, and when that happens you're going to run into a situation where your ActiveRecord models will refuse to work with old data that is no longer valid. Commands don't absolve you of the need to migrate your data when business requirements change, but they do let you move validation to the interaction where it belongs.

Commands can also be used on forms in place of ActiveRecord models, when the ActiveModel::Naming interface is included in the command class. Imperator doesn't have a dependency on ActiveRecord/ActiveModel/ActiveSupport, but readily supports most of the AM interfaces like validations. These are trivial to include so support is not built into Imperator intentionally.

###TODO

  • test coverage—Imperator was extracted out of some other work, and coverage was a part of those test suites.
  • Ensure compatibility with DJ, Resque and Sidekiq

#Using Imperator

##Requirements:

##Installation In your Gemfile:

gem 'imperator'

##Usage

###Creating the command:

class DoSomethingCommand < Imperator::Command
  attribute :some_object_id
  attribute :some_value

  validates_presence_of :some_object_id

  action do
    obj = SomeObject.find(self.some_object_id)
    obj.do_something(self.some_value)
    end
  end
end

###Using a command on a form builder First, you'll need to make your commands adhere to ActiveModel's naming interface:

#Gemfile
gem "activemodel"

#commands
class DoSomethingCommand < Imperator::Command
  include ActiveModel::Naming
  #...
end

Then you can use them on a form just as you would a model:

<%= form_for(@command, :as => :do_something, :url => some_resource_path(@command.some_object_id), :method => :put) do |f| %>
...
<% end %>

###Using a command class SomeController < ApplicationController def update command = DoSomethingCommand.new(params[:do_something]) if command.valid? command.perform redirect_to root_url else render edit_some_resource_path(command.some_object_id) end end end

###Using a command in the background (Delayed::Job) Delayed::Job.enqueue command

###Using a background processor You can create a custom background processor very easily. It merely needs to implement the class method #commit

class Imperator::NullBackgroundProcessor
  def self.commit(command, options = nil)
    command.perform
  end
end

You can also pass options to the background processor either by the command or instance

class CompletePurchase < Imperator::Command
  background :queue => "high"
  
  def action
  end
end

command = CompletePurchase.new
command.commit(:queue => "low")

###Contributors Many thanks to the following contributors for bugfixes, testing, and additional functionality

  • Jason Staten (@statianzo)
  • Jay Adkisson (@jayferd)
  • Nick Maloney (@nmaloney)

imperator's People

Contributors

karmajunkie avatar jneen avatar statianzo avatar ngmaloney avatar

Watchers

Hanfei Shen avatar James Cloos 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.