Our great sponsors
-
You can implement the service pattern as a PORO, but it helps to use one of the gems available to aid in code structure, orchestration, and error handling. The LightService gem provides a nice implementation without adding hardly any overhead. It’s design is very simple, as shown in the diagram below. An organizer is used to define the sequential workflow, which is made up of one or more actions. An error in any of the actions will short-circuit the entire workflow.
-
Consider the following organizer code from my sample news application. It has no comments, but it's pretty clear from the code what is happening.
-
PopRuby
PopRuby: Clothing and Accessories for Ruby Developers. Fashion meets Ruby! Shop our fun Ruby-inspired apparel and accessories designed to celebrate the joy and diversity of the Ruby community.
-
The context serves as a bag of attributes that is passed between actions. Each action specifies what attributes it expects as inputs, and what it promises to populate as outputs (if any). The context also includes success indicators and error messages. The code below is used to implement the AnalyzeSentimentAction using the TextMood gem to perform the actual analysis.