Read this blog post first: Veil Objects to Replace DTOs
First, install it:
$ gem install veils
Then, use it like this:
require 'veil'
obj = Veil.new(obj, to_s: 'Hello, world!')
The method to_s
will return Hello, world!
until some other
method is called and the veil is "pierced."
You can also use Unpiercable
decorator, which will never be pierced:
a very good instrument for data memoization.
You can also try AlterOut
, which lets you modify the output
of object methods on fly:
require 'alterout'
obj = AlterOut.new(obj, to_s: proc { |s| s + 'extra tail' })
There is also AlterIn
decorator, to modify incoming method arguments
(the result of the proc
will replace the list of input arguments):
require 'alterin'
obj = AlterIn.new(obj, print: proc { |i| [i + 1] })
Keep in mind that all classes are thread-safe.
Read these guidelines. Make sure you build is green before you contribute your pull request. You will need to have Ruby 2.3+ and Bundler installed. Then:
$ bundle update
$ bundle exec rake
If it's clean and you don't see any error messages, submit your pull request.