tiny_decorator 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +15 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +21 -0
- data/README.md +82 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/tiny_decorator/composite_decorator.rb +97 -0
- data/lib/tiny_decorator/single_decorator.rb +41 -0
- data/lib/tiny_decorator/version.rb +3 -0
- data/lib/tiny_decorator.rb +7 -0
- data/tiny_decorator.gemspec +39 -0
- metadata +105 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 556129b98566d8008a5debe82b1b482fdca17b671a7f5216efe52b6cb3c133a9
|
|
4
|
+
data.tar.gz: bbe8d99109236b1af9f6b0627e7efb5de0bb609a02a3113fbb940812da8e71eb
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: a7c87eab3b528b9cbbdc1cb63550a2d64bcfff06bfe15bc5af5db13a3abdce1153ec09615c96bbeb56cb06c2cc4c73c8e0a52f7841134d9c231585e5ee97b8aa
|
|
7
|
+
data.tar.gz: 8decc6dd24638f49b3c71a4165a9d92353689943b4ee48accf6514188256d35ed7d9195f445da1c219b66f277dd2d24f1652dd2b651770d1e9d4ef9d8054ffc4
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-gemset
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
tiny_decorator
|
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
2.5
|
data/.travis.yml
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
sudo: false
|
|
2
|
+
language: ruby
|
|
3
|
+
rvm:
|
|
4
|
+
- 2.5
|
|
5
|
+
before_install: gem install bundler -v 1.17.1
|
|
6
|
+
script:
|
|
7
|
+
- bundle exec rspec
|
|
8
|
+
deploy:
|
|
9
|
+
provider: rubygems
|
|
10
|
+
api_key:
|
|
11
|
+
secure: VfAdlKJmGqIjszXJBt20n3u/l0seyZNWbfrOTMTjAfL7zeIqGEEE82VVXJPXpZukH1/2n61MxmSCwfqXosfnQ7bjYOTXloIVNY8A8KHDC0BJBmzNN1Ex6neFoEl33Gw8+0pVToUiJpDbw8RUSND9yyNDM9P44BCS9yRoYPRKmb8kdyf57y4lB8d+guPPl6O/SwJ5t9KpazNEQYJuWmSzY5hebooVgp4ZjD/GH3XbsgzdGo/Qrv4D1Ie782J6QO/KPCbFIx6/EBwi4iDjWSp843bto5UVSlHY0HNcr9jmUxV/Egom/ISgizZNDjPgmuQNzpkgzPdYPnKg68iFZDY7Qhik9RPQTRotCYPqk9sGu2W3E9dYEIfD4ddMnl5bWvufS7x2z/B3t4fEw44be1Tn9VuBDWn7SyJU140GXz8SxvzyAz6UUFBtA+02g7S+Tci6P63G1S65pccFUc/T5+pgfYwA+7ykUiSIWYQaJx0SYJPrk2yOphFJmFfQp6wF25K177C6uwJvSof7PhHcLJiwB6O5qSpHx8O70M4zPcZcgrKRMWPh+PJfIyukif/Jxp5CCyaYwGvbqg1Ox9M6/SonsbNBgBefclJziIgTWre5nXjSHZaEW5QNdprGyBEoF1aTpR/lRozutuX5PgxyM4NfZ4zGvbkOAnZ6rncza0zurnk=
|
|
12
|
+
gem: tiny_decorator
|
|
13
|
+
on:
|
|
14
|
+
tags: true
|
|
15
|
+
repo: anvox/tiny_decorator
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
|
10
|
+
orientation.
|
|
11
|
+
|
|
12
|
+
## Our Standards
|
|
13
|
+
|
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
|
15
|
+
include:
|
|
16
|
+
|
|
17
|
+
* Using welcoming and inclusive language
|
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
|
19
|
+
* Gracefully accepting constructive criticism
|
|
20
|
+
* Focusing on what is best for the community
|
|
21
|
+
* Showing empathy towards other community members
|
|
22
|
+
|
|
23
|
+
Examples of unacceptable behavior by participants include:
|
|
24
|
+
|
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
+
advances
|
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
28
|
+
* Public or private harassment
|
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
|
30
|
+
address, without explicit permission
|
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
+
professional setting
|
|
33
|
+
|
|
34
|
+
## Our Responsibilities
|
|
35
|
+
|
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
+
response to any instances of unacceptable behavior.
|
|
39
|
+
|
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
44
|
+
threatening, offensive, or harmful.
|
|
45
|
+
|
|
46
|
+
## Scope
|
|
47
|
+
|
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
+
when an individual is representing the project or its community. Examples of
|
|
50
|
+
representing a project or community include using an official project e-mail
|
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
|
53
|
+
further defined and clarified by project maintainers.
|
|
54
|
+
|
|
55
|
+
## Enforcement
|
|
56
|
+
|
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
+
reported by contacting the project team at thien.an.vo.nguyen@gmail.com. All
|
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
|
63
|
+
|
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
|
66
|
+
members of the project's leadership.
|
|
67
|
+
|
|
68
|
+
## Attribution
|
|
69
|
+
|
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: http://contributor-covenant.org
|
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
tiny_decorator (0.1.0)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
diff-lcs (1.3)
|
|
10
|
+
rake (10.5.0)
|
|
11
|
+
rspec (3.8.0)
|
|
12
|
+
rspec-core (~> 3.8.0)
|
|
13
|
+
rspec-expectations (~> 3.8.0)
|
|
14
|
+
rspec-mocks (~> 3.8.0)
|
|
15
|
+
rspec-core (3.8.0)
|
|
16
|
+
rspec-support (~> 3.8.0)
|
|
17
|
+
rspec-expectations (3.8.2)
|
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
19
|
+
rspec-support (~> 3.8.0)
|
|
20
|
+
rspec-mocks (3.8.0)
|
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
22
|
+
rspec-support (~> 3.8.0)
|
|
23
|
+
rspec-support (3.8.0)
|
|
24
|
+
|
|
25
|
+
PLATFORMS
|
|
26
|
+
ruby
|
|
27
|
+
|
|
28
|
+
DEPENDENCIES
|
|
29
|
+
bundler (~> 1.16)
|
|
30
|
+
rake (~> 10.0)
|
|
31
|
+
rspec (~> 3.0)
|
|
32
|
+
tiny_decorator!
|
|
33
|
+
|
|
34
|
+
BUNDLED WITH
|
|
35
|
+
1.17.1
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 An Vo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
|
|
2
|
+
[](https://travis-ci.org/anvox/tiny_decorator)
|
|
3
|
+
|
|
4
|
+
# TinyDecorator
|
|
5
|
+
|
|
6
|
+
The world of ruby decorator is dominated by `draper`, I use it all the time.
|
|
7
|
+
But when project grows and we chase the dealine, the flexible and easily of `draper` make us keep abusing it. Now, when using a decorated model we dont know which method/attribute are define in which decorator, which decorators depend on which ones.
|
|
8
|
+
`tiny_decorator` aims to separate decoration logic into different decorators, and centralize logic to determine which decorators are used. It isn't built to replace `draper` as it just solve our internal problem of abusing `draper`, it's better if you using `draper` wisely instead `tiny_decorator`
|
|
9
|
+
|
|
10
|
+
But if you're interesting in this gem, every contributes are welcome.
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
Add this line to your application's Gemfile:
|
|
15
|
+
|
|
16
|
+
```ruby
|
|
17
|
+
gem 'tiny_decorator'
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
And then execute:
|
|
21
|
+
|
|
22
|
+
$ bundle
|
|
23
|
+
|
|
24
|
+
Or install it yourself as:
|
|
25
|
+
|
|
26
|
+
$ gem install tiny_decorator
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
Model decorated by 1 single decorator
|
|
31
|
+
```ruby
|
|
32
|
+
class NewDecorator < TinyDecorator::SingleDecorator
|
|
33
|
+
def new_method
|
|
34
|
+
'decorated'
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
NewDecorator.decorate(Object.new).new_method # 'decorated'
|
|
39
|
+
NewDecorator.decorate_collection([Object.new, Object.new])[1].new_method # 'decorated'
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Model decorate by more than 1 decorators
|
|
43
|
+
```ruby
|
|
44
|
+
class NewDecorator2
|
|
45
|
+
extend TinyDecorator::CompositeDecorator
|
|
46
|
+
|
|
47
|
+
decorated_by :default, 'DefaultDecorator'
|
|
48
|
+
|
|
49
|
+
decorated_by :nil, ->(record) { record.nil? ? 'NilDecorator' : 'DefaultDecorator' }
|
|
50
|
+
decorated_by :nil, ->(record, context) { record.in(context).nil? ? 'NilDecorator' : 'DefaultDecorator' }
|
|
51
|
+
end
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
And use it
|
|
55
|
+
```ruby
|
|
56
|
+
context = { current_user: user }
|
|
57
|
+
#
|
|
58
|
+
NewDecorator.decorate(model)
|
|
59
|
+
NewDecorator.decorate(model, context)
|
|
60
|
+
# OR
|
|
61
|
+
NewDecorator.decorate_collection(models)
|
|
62
|
+
NewDecorator.decorate_collection(models, context)
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Development
|
|
67
|
+
|
|
68
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
69
|
+
|
|
70
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
71
|
+
|
|
72
|
+
## Contributing
|
|
73
|
+
|
|
74
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/anvox/tiny_decorator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
|
75
|
+
|
|
76
|
+
## License
|
|
77
|
+
|
|
78
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
79
|
+
|
|
80
|
+
## Code of Conduct
|
|
81
|
+
|
|
82
|
+
Everyone interacting in the TinyDecorator project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/anvox/tiny_decorator/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "tiny_decorator"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
module TinyDecorator
|
|
2
|
+
#
|
|
3
|
+
# Passing only decorator name, object will be decorated
|
|
4
|
+
# ```
|
|
5
|
+
# extend TinyDecorator::CompositeDecorator
|
|
6
|
+
# decorated_by :default, 'DefaultDecorator'
|
|
7
|
+
# ```
|
|
8
|
+
#
|
|
9
|
+
# Passing only 1 block param to #decorated_by
|
|
10
|
+
# Execute block to get decorator name.
|
|
11
|
+
# ```
|
|
12
|
+
# extend TinyDecorator::CompositeDecorator
|
|
13
|
+
# decorated_by :default, ->(record) { record.nil? ? 'NilDecorator' : 'DefaultDecorator' }
|
|
14
|
+
# decorated_by :default, ->(record, context) { record.in(context).nil? ? 'NilDecorator' : 'DefaultDecorator' }
|
|
15
|
+
# ```
|
|
16
|
+
#
|
|
17
|
+
# Passing decorator name and 1 block param to #decorated_by
|
|
18
|
+
# Execute block to determine should we decorate it or not
|
|
19
|
+
# ```
|
|
20
|
+
# extend TinyDecorator::CompositeDecorator
|
|
21
|
+
# decorated_by :default, ->(record) { record.valid? ? 'ValidDecorator' : 'InvalidDecorator' }
|
|
22
|
+
# decorated_by :default, ->(record, context) { record.in(context).valid? ? 'ValidDecorator' : 'InvalidDecorator' }
|
|
23
|
+
# ```
|
|
24
|
+
#
|
|
25
|
+
# For now, `:name` is redundant, but it's nice to have a friendly name for readbility and later usage
|
|
26
|
+
#
|
|
27
|
+
# Block may receive
|
|
28
|
+
# 1 parameter is record to decorate OR
|
|
29
|
+
# 2 parameters are record to decorate and context
|
|
30
|
+
#
|
|
31
|
+
# CompositeDecorator introduces a central conditional decorator manager.
|
|
32
|
+
# It answer the questions: which decorater will be used in which conditions.
|
|
33
|
+
# The decorator is a sub class of TinyDecorator::BaseDelegator (or draper decorator, but not recommend)
|
|
34
|
+
# TinyDecorator::BaseDelegator will answer the question which attributes are decorated.
|
|
35
|
+
#
|
|
36
|
+
module CompositeDecorator
|
|
37
|
+
# Decorate collection of objects, each object is decorate by `#decorate`
|
|
38
|
+
# TODO: [AV] It's greate if with activerecord relationshop, we defer decorate until data retrieved.
|
|
39
|
+
# Using `map` will make data retrieval executes immediately
|
|
40
|
+
def decorate_collection(records, context = {})
|
|
41
|
+
Array(records).map do |record|
|
|
42
|
+
decorate(record, context)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Decorate an object by defined `#decorated_by`
|
|
47
|
+
def decorate(record, context = {})
|
|
48
|
+
if instance_variable_get(:@_contexts)
|
|
49
|
+
context = context.merge(instance_variable_get(:@_contexts).inject({}) do |carry, (context_name, context_block)|
|
|
50
|
+
context[context_name] = context_block.call(record, context)
|
|
51
|
+
|
|
52
|
+
carry
|
|
53
|
+
end)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
instance_variable_get(:@decorators).inject(record) do |carry, (name, value)|
|
|
57
|
+
decorator = decorator_resolver(name, value, record, context)
|
|
58
|
+
if decorator
|
|
59
|
+
carry = begin
|
|
60
|
+
const_get(decorator, false)
|
|
61
|
+
rescue NameError
|
|
62
|
+
Object.const_get(decorator, false)
|
|
63
|
+
end.decorate(carry, context)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
carry
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
private
|
|
71
|
+
|
|
72
|
+
def decorated_by(decorate_name, class_name, condition_block = nil)
|
|
73
|
+
decorators = instance_variable_get(:@decorators) || {}
|
|
74
|
+
decorators[decorate_name] = [class_name, condition_block]
|
|
75
|
+
instance_variable_set(:@decorators, decorators)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def set_context(context_name, context_block)
|
|
79
|
+
_contexts = instance_variable_get(:@_contexts) || {}
|
|
80
|
+
_contexts[context_name] = context_block
|
|
81
|
+
instance_variable_set(:@_contexts, _contexts)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Resolve decorator class from #decorated_by definition
|
|
85
|
+
# if 1st param is a block, evaluate it as decorator name
|
|
86
|
+
# if 1st param isn't a block
|
|
87
|
+
# if no 2nd param, 1st param is decorator name
|
|
88
|
+
# if 2nd param exists, evaluate it as boolean to determine decorate or not
|
|
89
|
+
def decorator_resolver(_name, value, record, context)
|
|
90
|
+
if value[0].respond_to?(:call)
|
|
91
|
+
((value[0].arity == 1 && value[0].call(record)) || (value[0].arity == 2 && value[0].call(record, context)))
|
|
92
|
+
elsif value[1].nil? || (value[1].arity == 1 && value[1].call(record)) || (value[1].arity == 2 && value[1].call(record, context))
|
|
93
|
+
value[0]
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
module TinyDecorator
|
|
2
|
+
# Single decorator. Inherite, then define methods to use.
|
|
3
|
+
# This base decorator delegates all. To limit delegation to source object.
|
|
4
|
+
#
|
|
5
|
+
# ```
|
|
6
|
+
# class NewDecorator < TinyDecorator::SingleDecorator
|
|
7
|
+
# def new_method
|
|
8
|
+
# 'decorated'
|
|
9
|
+
# end
|
|
10
|
+
# end
|
|
11
|
+
#
|
|
12
|
+
# NewDecorator.decorate(Object.new).new_method # 'decorated'
|
|
13
|
+
# NewDecorator.decorate_collection([Object.new, Object.new])[1].new_method # 'decorated'
|
|
14
|
+
# ```
|
|
15
|
+
#
|
|
16
|
+
# Use in case source has only 1 decorator.
|
|
17
|
+
# In case more than 1 decorator or conditional decorate, we prefer using `TinyDecorator::CompositeDecorator`
|
|
18
|
+
class SingleDecorator < SimpleDelegator
|
|
19
|
+
# Initialize, use `.decorate` or `.new` are the same
|
|
20
|
+
# @param delegatee [Object] source object to decorate
|
|
21
|
+
# @param context [Hash] context as hash, could read within decorator scope by `context[]`
|
|
22
|
+
def initialize(delegatee, context = {})
|
|
23
|
+
@context = context
|
|
24
|
+
__setobj__(delegatee)
|
|
25
|
+
end
|
|
26
|
+
attr_reader :context
|
|
27
|
+
|
|
28
|
+
class << self
|
|
29
|
+
# Decorate a collection, collection must extend Enum behavior
|
|
30
|
+
# @param delegatees [Array] collection of source object to decorate
|
|
31
|
+
# @param context [Hash] context as hash, could read within decorator scope by `context[]`
|
|
32
|
+
def decorate_collection(delegatees, context = {})
|
|
33
|
+
delegatees.map do |delegatee|
|
|
34
|
+
new(delegatee, context)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
alias decorate new
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
|
|
3
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
5
|
+
require "tiny_decorator/version"
|
|
6
|
+
|
|
7
|
+
Gem::Specification.new do |spec|
|
|
8
|
+
spec.name = "tiny_decorator"
|
|
9
|
+
spec.version = TinyDecorator::VERSION
|
|
10
|
+
spec.authors = ['An Vo']
|
|
11
|
+
spec.email = ['thien.an.vo.nguyen@gmail.com']
|
|
12
|
+
|
|
13
|
+
spec.summary = 'A simple decorator untility module'
|
|
14
|
+
spec.description = 'Inspire by draper. But draper is too overkill, and I want to avoid decorator abusing.'
|
|
15
|
+
spec.homepage = 'https://github.com/anvox/tiny_decorator'
|
|
16
|
+
spec.license = "MIT"
|
|
17
|
+
|
|
18
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
|
19
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
|
20
|
+
if spec.respond_to?(:metadata)
|
|
21
|
+
spec.metadata["allowed_push_host"] = 'https://rubygems.org'
|
|
22
|
+
else
|
|
23
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
|
24
|
+
"public gem pushes."
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Specify which files should be added to the gem when it is released.
|
|
28
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
29
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
30
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
31
|
+
end
|
|
32
|
+
spec.bindir = "exe"
|
|
33
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
34
|
+
spec.require_paths = ["lib"]
|
|
35
|
+
|
|
36
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
|
37
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
38
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
39
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: tiny_decorator
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- An Vo
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2019-02-01 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.16'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.16'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '3.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '3.0'
|
|
55
|
+
description: Inspire by draper. But draper is too overkill, and I want to avoid decorator
|
|
56
|
+
abusing.
|
|
57
|
+
email:
|
|
58
|
+
- thien.an.vo.nguyen@gmail.com
|
|
59
|
+
executables: []
|
|
60
|
+
extensions: []
|
|
61
|
+
extra_rdoc_files: []
|
|
62
|
+
files:
|
|
63
|
+
- ".gitignore"
|
|
64
|
+
- ".rspec"
|
|
65
|
+
- ".ruby-gemset"
|
|
66
|
+
- ".ruby-version"
|
|
67
|
+
- ".travis.yml"
|
|
68
|
+
- CODE_OF_CONDUCT.md
|
|
69
|
+
- Gemfile
|
|
70
|
+
- Gemfile.lock
|
|
71
|
+
- LICENSE.txt
|
|
72
|
+
- README.md
|
|
73
|
+
- Rakefile
|
|
74
|
+
- bin/console
|
|
75
|
+
- bin/setup
|
|
76
|
+
- lib/tiny_decorator.rb
|
|
77
|
+
- lib/tiny_decorator/composite_decorator.rb
|
|
78
|
+
- lib/tiny_decorator/single_decorator.rb
|
|
79
|
+
- lib/tiny_decorator/version.rb
|
|
80
|
+
- tiny_decorator.gemspec
|
|
81
|
+
homepage: https://github.com/anvox/tiny_decorator
|
|
82
|
+
licenses:
|
|
83
|
+
- MIT
|
|
84
|
+
metadata:
|
|
85
|
+
allowed_push_host: https://rubygems.org
|
|
86
|
+
post_install_message:
|
|
87
|
+
rdoc_options: []
|
|
88
|
+
require_paths:
|
|
89
|
+
- lib
|
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
91
|
+
requirements:
|
|
92
|
+
- - ">="
|
|
93
|
+
- !ruby/object:Gem::Version
|
|
94
|
+
version: '0'
|
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
|
+
requirements:
|
|
97
|
+
- - ">="
|
|
98
|
+
- !ruby/object:Gem::Version
|
|
99
|
+
version: '0'
|
|
100
|
+
requirements: []
|
|
101
|
+
rubygems_version: 3.0.2
|
|
102
|
+
signing_key:
|
|
103
|
+
specification_version: 4
|
|
104
|
+
summary: A simple decorator untility module
|
|
105
|
+
test_files: []
|