Those are application patterns recommended and frequently used by me in my apps. The aim is not to provide the standard GOF patterns, but real life snippets for scenarios I came across frequently.
Some of the examples, might not be the simplest to follow, this might be due the fact, that I was trying to make them no the typical over-simplified blog examples, but close to real life situations.
All examples can be found in the specs. The implementations are kept within the specs - this is the way I always start an implementation, first write spec and implementation in one file, only if it's done, move it to its own file and include in the general codebase.
Errors should be nested in a module in your library or app.
module RubyPatterns
class MyError < StandardError; end
endIn the describe prefer adding a type instead of a string description for unit tests. Here I refer to RubyPatters::MyError to make sure the class actually exists. Also use described_class to refer to that class. If you change the name, you have to change it only in one place.
describe RubyPatterns::MyError do
specify{
expect { raise described_class }.to_raise described_class
}
endReferences:
- small units of work
- one class or module per activity (SRP)
Now Ruby 2.1 has brought the #cause method with the inner exception, if you're on an earlier version of ruby, use this:
class ParseParamsError < StandardError
attr_reader :inner
def initialize(msg, inner=$ERROR_INFO)
super(msg)
@inner = inner
end
endReferences:
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request