https://github.com/yegor256/veils
Ruby Veil Objects
https://github.com/yegor256/veils
caching object-oriented-programming ruby ruby-gem
Last synced: about 1 year ago
JSON representation
Ruby Veil Objects
- Host: GitHub
- URL: https://github.com/yegor256/veils
- Owner: yegor256
- License: mit
- Created: 2020-05-08T05:14:30.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-05-22T11:41:38.000Z (about 2 years ago)
- Last Synced: 2024-05-22T12:47:15.802Z (about 2 years ago)
- Topics: caching, object-oriented-programming, ruby, ruby-gem
- Language: Ruby
- Homepage: https://www.yegor256.com/2020/05/19/veil-objects.html
- Size: 94.7 KB
- Stars: 14
- Watchers: 4
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README

[](https://www.elegantobjects.org)
[](http://www.rultor.com/p/yegor256/veils)
[](https://www.jetbrains.com/ruby/)
[](https://github.com/yegor256/veils/actions/workflows/rake.yml)
[](http://badge.fury.io/rb/veils)
[](https://codeclimate.com/github/yegor256/veils/maintainability)
[](http://rubydoc.info/github/yegor256/veils/master/frames)
[](https://hitsofcode.com/view/github/yegor256/veils)
[](https://github.com/yegor256/veils/blob/master/LICENSE.txt)
Read this blog post first:
[Veil Objects to Replace DTOs](https://www.yegor256.com/2020/05/19/veil-objects.html)
First, install it:
```bash
$ gem install veils
```
Then, use it like this:
```ruby
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:
```ruby
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):
```ruby
require 'alterin'
obj = AlterIn.new(obj, print: proc { |i| [i + 1] })
```
Keep in mind that all classes are thread-safe.
## How to contribute
Read [these guidelines](https://www.yegor256.com/2014/04/15/github-guidelines.html).
Make sure your build is green before you contribute
your pull request. You will need to have [Ruby](https://www.ruby-lang.org/en/) 2.3+ and
[Bundler](https://bundler.io/) installed. Then:
```
$ bundle update
$ bundle exec rake
```
If it's clean and you don't see any error messages, submit your pull request.