pay_dirt
A Ruby gem based on the "use case" pattern set forth in opencurriculum-flashcards
Provides the basic building blocks of a pattern capable of reducing a towering codebase to modular rubble (or more Ruby gems)
There are two ways to employ the pattern:
- use a class that inherits from PayDirt::Base
- use a class or module that includes PayDirt::UseCase
service object generator
pay_dirt now provides a service object generator, powered by thor. In order to use them in your rails app, you'll need to install the task. Here's how:
$ thor install https://raw.github.com/rthbound/pay_dirt/master/pay_dirt.thor
...
Do you wish to continue [y/N]? y
Please specify a name for https://raw.github.com/rthbound/pay_dirt/master/pay_dirt.thor in the system repository [pay_dirt.thor]: pay_dirt
Storing thor file in your system repository
$
After installing, you can use your new generator anywhere you can use thor. It'll tell you how it's used:
$ thor help pay_dirt:service_object:new
Usage:
thor pay_dirt:service_object:new FILE -d, --dependencies=one two three
Options:
-d, --dependencies=one two three # specify required dependencies
-D, [--defaults=key:value] # specify default dependencies
-i, [--inherit] # inherit from PayDirt::Base class
# Default: true
-m, [--include] # include the PayDirt::UseCase module (overrides --inherit)
create a service object
example
$ thor pay_dirt:service_object:new digit_check -d fingers toes -D fingers:10 toes:10
create lib/service_objects/digit_check.rb
Running the above generator will create the following file
require 'pay_dirt'
module ServiceObjects
class DigitCheck < PayDirt::Base
def initialize( = {})
= {
fingers: 10,
toes: 10,
}.merge()
(:fingers, :toes, )
end
def execute!
result(true)
end
end
end
Usage:
require "service_objects/digit_check" #=> true
ServiceObjects::DigitCheck.new.execute!
#=> #<PayDirt::Result:0xa0be85c @data=nil, @success=true>
As you can see, we can now call ServiceObjects::DigitCheck.new(fingers: 10, toes: 10).execute!
and expect a successful return object. Where you take it from there is up to you.