Saturday, March 20, 2021

Strategic 0.9.1 Strategy Matcher/Alias/Exclusion

The Strategic Ruby gem (Painless Strategy Pattern in Ruby and Rails) just had a new release in 0.9.1 (as well as 0.9.0), adding the following features:

  • Strategy Matcher: custom matcher support for selecting strategies (e.g. implement a fuzz matcher for picking a strategy by full string name or any partial match in case it is typed by a user)
  • Strategy Alias: specify an alias for selecting a strategy (e.g. car strategy has "sedan" as an alias)
  • Strategy Exclusion: exclude a strategy from a matcher (e.g. partial match on 'USA' and 'US', but not 'U')
To give you some background, the Strategic Ruby gem was mostly born out of work I did last year at Chronogolf by Lightspeed, a golf course management web app built in Ruby on Rails, where we had countless of strategies for customizing models, especially in relation to quotes, pricing, payments, memberships, and golf course tee time reservations. 

It is currently used in the DCR Programming Language, implementing language commands (Command Pattern) as a special case of Strategy Pattern, with auto-inference of strategy names from command file names by convention.

Strategic enables you to make any existing domain model "strategic", externalizing all logic concerning algorithmic variations into separate strategy classes that are easy to find, maintain and extend while honoring the Open/Closed Principle and avoiding conditionals.

In summary, if you make a class called TaxCalculator strategic by including the Strategic mixin module, now you are able to drop strategies under the tax_calculator directory sitting next to the class (e.g. tax_calculator/us_strategy.rb, tax_calculator/canada_strategy.rb) while gaining extra API methods to grab strategy names to present in a user interface, grab strategy classes to select, and/or instantiate TaxCalculator directly with a strategy from the get-go.

You may also read:

No comments: