decorations 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 76ada36da11eb08f3bf1265836f6e9188b43c94c2dcb3d8622aa1d6a81bdfac3
4
+ data.tar.gz: 4b7be5cd0308efeed59e4164f69cd0c8e8838dddb59a8e8611103c4c30fb8b62
5
+ SHA512:
6
+ metadata.gz: 87841ddc81602fcaad6a6886a06446c09da3e4d87117ba380cfe1fd792d2c481eef45e09d5e4cffc55a2c0024c1148dbf19ceecc3970417337f0356f6ad94994
7
+ data.tar.gz: c969aaaf45675909b3f6fb0daae2b34ede7e4e4a392fe69cec9c65ad2434b9d04d35444b1f6a40e73d670d373a68dec4d11baacfa19015d9de255e1a3540253d
data/.gitignore ADDED
@@ -0,0 +1,66 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # Ignore Vim swaps
50
+ *.sw*
51
+
52
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
53
+ .rvmrc
54
+ =======
55
+ /.bundle/
56
+ /.yardoc
57
+ /_yardoc/
58
+ /coverage/
59
+ /doc/
60
+ /out/
61
+ /pkg/
62
+ /spec/reports/
63
+ /tmp/
64
+
65
+ # rspec failure tracking
66
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,12 @@
1
+ ---
2
+
3
+ AllCops:
4
+ TargetRubyVersion: 2.3
5
+
6
+ Metrics/LineLength:
7
+ Max: 120
8
+
9
+ Metrics/BlockLength:
10
+ Exclude:
11
+ - '*.gemspec'
12
+ - '**/*_spec.rb'
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.2
7
+ before_install: gem install bundler -v 1.17.2
@@ -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 john@shields.wtf. 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
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in decorations.gemspec
8
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,93 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ decorations (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ coderay (1.1.2)
11
+ coveralls (0.7.2)
12
+ multi_json (~> 1.3)
13
+ rest-client (= 1.6.7)
14
+ simplecov (>= 0.7)
15
+ term-ansicolor (= 1.2.2)
16
+ thor (= 0.18.1)
17
+ diff-lcs (1.3)
18
+ docile (1.3.2)
19
+ gem-release (2.1.1)
20
+ jaro_winkler (1.5.4)
21
+ json (2.2.0)
22
+ method_source (0.9.2)
23
+ mime-types (3.3)
24
+ mime-types-data (~> 3.2015)
25
+ mime-types-data (3.2019.1009)
26
+ multi_json (1.14.1)
27
+ parallel (1.18.0)
28
+ parser (2.6.5.0)
29
+ ast (~> 2.4.0)
30
+ pry (0.12.2)
31
+ coderay (~> 1.1.0)
32
+ method_source (~> 0.9.0)
33
+ pry-doc (1.0.0)
34
+ pry (~> 0.11)
35
+ yard (~> 0.9.11)
36
+ rainbow (3.0.0)
37
+ rake (10.5.0)
38
+ rest-client (1.6.7)
39
+ mime-types (>= 1.16)
40
+ rspec (3.9.0)
41
+ rspec-core (~> 3.9.0)
42
+ rspec-expectations (~> 3.9.0)
43
+ rspec-mocks (~> 3.9.0)
44
+ rspec-core (3.9.0)
45
+ rspec-support (~> 3.9.0)
46
+ rspec-expectations (3.9.0)
47
+ diff-lcs (>= 1.2.0, < 2.0)
48
+ rspec-support (~> 3.9.0)
49
+ rspec-mocks (3.9.0)
50
+ diff-lcs (>= 1.2.0, < 2.0)
51
+ rspec-support (~> 3.9.0)
52
+ rspec-support (3.9.0)
53
+ rubocop (0.76.0)
54
+ jaro_winkler (~> 1.5.1)
55
+ parallel (~> 1.10)
56
+ parser (>= 2.6)
57
+ rainbow (>= 2.2.2, < 4.0)
58
+ ruby-progressbar (~> 1.7)
59
+ unicode-display_width (>= 1.4.0, < 1.7)
60
+ ruby-progressbar (1.10.1)
61
+ simplecov (0.17.1)
62
+ docile (~> 1.1)
63
+ json (>= 1.8, < 3)
64
+ simplecov-html (~> 0.10.0)
65
+ simplecov-html (0.10.2)
66
+ term-ansicolor (1.2.2)
67
+ tins (~> 0.8)
68
+ thor (0.18.1)
69
+ tins (0.13.2)
70
+ unicode-display_width (1.6.0)
71
+ yard (0.9.20)
72
+ yardstick (0.9.9)
73
+ yard (~> 0.8, >= 0.8.7.2)
74
+
75
+ PLATFORMS
76
+ ruby
77
+
78
+ DEPENDENCIES
79
+ bundler (~> 1.16)
80
+ coveralls
81
+ decorations!
82
+ gem-release
83
+ pry
84
+ pry-doc
85
+ rake (~> 10.0)
86
+ rspec (~> 3.0)
87
+ rubocop
88
+ simplecov
89
+ yard
90
+ yardstick
91
+
92
+ BUNDLED WITH
93
+ 1.17.2
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 John Shields
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 John Shields
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,74 @@
1
+ # Decorations
2
+
3
+ Python like decorators for Ruby. Inspired by Rack and previous attempts at decorations
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'decorations'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install decorations
20
+
21
+ ## Usage
22
+
23
+ Create a class that will have decorated methods and create decator classes to before actions.
24
+
25
+ The decorator class must:
26
+ * implement #call
27
+ * must invoke call\_next(this, chain, \*args)
28
+ * must return the result of call\_next.
29
+
30
+ ```ruby
31
+ require 'decorations'
32
+
33
+ class LoggingDecorator < Decorator
34
+ def call(this, chain, *args)
35
+ puts "[#{Time.now}] #{decorated_class}.#{decorated_method.name} was called"
36
+ result = call_next(this, chain, *args)
37
+ puts "[#{Time.now}] #{decorated_class}.#{decorated_method.name} has finished"
38
+ result
39
+ end
40
+ end
41
+
42
+ class Application
43
+ extend Decorations
44
+
45
+ decorate LoggingDecorator
46
+ def perform_task
47
+ 2 + 2
48
+ end
49
+ end
50
+
51
+ app = Application.new
52
+ app.perform_task
53
+ # => [2019-10-31 02:00:31 -0700] LoggingDecorator.perform_task was called
54
+ # => [2019-10-31 02:00:31 -0700] LoggingDecorator.perform_task has finished
55
+ # => 4
56
+ ```
57
+
58
+ ## Development
59
+
60
+ 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.
61
+
62
+ 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).
63
+
64
+ ## Contributing
65
+
66
+ Bug reports and pull requests are welcome on GitHub at https://github.com/watmin/Ruby-decorations. 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.
67
+
68
+ ## License
69
+
70
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
71
+
72
+ ## Code of Conduct
73
+
74
+ Everyone interacting in the Decorations project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/watmin/Ruby-decorations/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop/rake_task'
6
+ require 'yard'
7
+ require 'yard/rake/yardoc_task'
8
+ require 'yardstick/rake/measurement'
9
+ require 'yardstick/rake/verify'
10
+
11
+ def empty_directory(directory)
12
+ FileUtils.remove_entry(directory)
13
+ FileUtils.mkdir(directory)
14
+ end
15
+
16
+ # clean up the workspace
17
+ task(:clean) do
18
+ empty_directory('doc')
19
+ empty_directory('out')
20
+ empty_directory('pkg')
21
+ end
22
+
23
+ # unit tests
24
+ RSpec::Core::RakeTask.new(:spec) do |task|
25
+ task.pattern = 'spec/**/*_spec.rb'
26
+ task.rspec_opts = '--require spec_helper ' \
27
+ '--format documentation ' \
28
+ '--format html ' \
29
+ '--out out/rspec_unit.html'
30
+ end
31
+ task(default: :spec)
32
+
33
+ # configure rubocop
34
+ RuboCop::RakeTask.new(:rubocop) do |task|
35
+ task.options = [
36
+ '--format', 'simple',
37
+ '--format', 'html',
38
+ '--out', 'out/rubocop.html',
39
+ 'lib/**/*.rb',
40
+ 'spec/**/*.rb'
41
+ ]
42
+ end
43
+ task(default: :rubocop)
44
+
45
+ # configure yard
46
+ YARD::Rake::YardocTask.new(:yard) do |task|
47
+ task.files = Dir['lib/**/*.rb']
48
+ task.options = [
49
+ '--quiet' # YARD doesn't like the methods that received (**args)
50
+ ]
51
+ task.stats_options = [
52
+ '--list-undoc'
53
+ ]
54
+ end
55
+ task(default: :yard)
56
+
57
+ # documentation coverage
58
+ Yardstick::Rake::Measurement.new(:yardstick_measure) do |task|
59
+ task.output = 'out/yard-report.txt'
60
+ end
61
+ task(default: :yardstick_measure)
62
+
63
+ # documentation enforcement
64
+ Yardstick::Rake::Verify.new(:yardstick_verify) do |task|
65
+ task.threshold = 100
66
+ end
67
+ task(default: :yardstick_verify)
68
+
69
+ # Require tests, syntax and documentation to pass before building
70
+ task(build: %i[spec rubocop yard yardstick_measure yardstick_verify])
71
+
72
+ # Require a succesful build before release
73
+ task(release: %i[build])
data/bin/console ADDED
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'decorations'
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
+ class DecoratorDemo < Decorator
10
+ def call(this, chain, *args)
11
+ puts "'#{decorated_class}' has '#{decorated_method.name}' decorated"
12
+ call_next(this, chain, *args)
13
+ end
14
+ end
15
+
16
+ class Demo
17
+ extend Decorations
18
+
19
+ decorate DecoratorDemo
20
+ def demo_method
21
+ puts 'am I decorated?'
22
+ end
23
+ end
24
+
25
+ require 'pry'
26
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'decorations/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'decorations'
9
+ spec.version = Decorations::VERSION
10
+ spec.authors = ['John Shields']
11
+ spec.email = ['john@shields.wtf']
12
+
13
+ spec.summary = 'Python like decorators for Ruby'
14
+ spec.homepage = 'https://github.com/watmin/Ruby-decorations'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.' unless spec.respond_to?(:metadata)
20
+
21
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_development_dependency 'bundler', '~> 1.16'
33
+ spec.add_development_dependency 'coveralls'
34
+ spec.add_development_dependency 'gem-release'
35
+ spec.add_development_dependency 'pry'
36
+ spec.add_development_dependency 'pry-doc'
37
+ spec.add_development_dependency 'rake', '~> 10.0'
38
+ spec.add_development_dependency 'rspec', '~> 3.0'
39
+ spec.add_development_dependency 'rubocop'
40
+ spec.add_development_dependency 'simplecov'
41
+ spec.add_development_dependency 'yard'
42
+ spec.add_development_dependency 'yardstick'
43
+ end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'decorations/version'
4
+ require 'decorator'
5
+
6
+ ##
7
+ # Decorations class to extend within your class
8
+ module Decorations
9
+ ##
10
+ # Injects the decorated_methods attribute in the class received
11
+ #
12
+ # @param receiver [Class] the class extending Decorations
13
+ #
14
+ # @return [Void]
15
+ #
16
+ # @api private
17
+ def self.extended(receiver)
18
+ class << receiver
19
+ attr_accessor :decorated_methods
20
+ end
21
+ end
22
+
23
+ ##
24
+ # Decorates a method execute's the klass' #call method around the decorated method
25
+ #
26
+ # @return [Void]
27
+ #
28
+ # @example
29
+ # class DecoratorDemo < Decorator
30
+ # def call(this, chain, *args)
31
+ # puts "'#{decorated_class}' has '#{decorated_method.name}' decorated"
32
+ # call_next(this, chain, *args)
33
+ # end
34
+ # end
35
+ #
36
+ # class Demo
37
+ # extend Decorations
38
+ #
39
+ # decorate DecoratorDemo
40
+ # def demo_method
41
+ # puts 'am I decorated?'
42
+ # end
43
+ # end
44
+ #
45
+ # demo = Demo.new
46
+ # demo.demo_method
47
+ # # => 'DecoratorDemo' has 'demo_method' decorated
48
+ # # => am I decorated?
49
+ #
50
+ # @api public
51
+ def decorate(klass, *args)
52
+ @decorators ||= []
53
+ @decorators << [klass, args]
54
+ end
55
+
56
+ private
57
+
58
+ ##
59
+ # Appends the decorators to the decorated_methods hash
60
+ #
61
+ # @param name [Symbol] the method name being decorated
62
+ # @param decorators [Array<Decorator>] the decorators defined
63
+ # @param decorated_methods [Hash] the decorated methods for the class
64
+ #
65
+ # @return [Void]
66
+ #
67
+ # @api private
68
+ def append_decorations(name, decorators, decorated_methods)
69
+ decorators.each do |klass, args|
70
+ decorated_methods[name] << klass.new(klass, instance_method(name), *args)
71
+ end
72
+ end
73
+
74
+ ##
75
+ # Hooks the methods defined for a class
76
+ #
77
+ # @param name [Symbol] the name of the method being defined
78
+ #
79
+ # @return [Void]
80
+ #
81
+ # @api private
82
+ def method_added(name)
83
+ return unless @decorators
84
+
85
+ @decorated_methods ||= Hash.new { |h, k| h[k] = [] }
86
+ append_decorations(name, @decorators, @decorated_methods)
87
+ @decorators = nil
88
+
89
+ class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
90
+ def #{name}(*args, &blk)
91
+ chain = self.class.decorated_methods[#{name.inspect}].dup
92
+ chain.first.call_next(self, chain, *args, &blk)
93
+ end
94
+ RUBY_EVAL
95
+ end
96
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decorations
4
+ ##
5
+ # The version of the Ruby-decorations gem
6
+ VERSION = '0.1.0'
7
+ end
data/lib/decorator.rb ADDED
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # Base decorator class
5
+ class Decorator
6
+ ##
7
+ # @!attribute [r] decorated_class
8
+ # The class being decorated
9
+ #
10
+ # @return [Class]
11
+ #
12
+ # @example
13
+ # decorator.decoated_class #=> Class
14
+ #
15
+ # @api public
16
+ attr_reader :decorated_class
17
+
18
+ ##
19
+ # @!attribute [r] decorated_class
20
+ # The method being decorated
21
+ #
22
+ # @return [Symbol]
23
+ #
24
+ # @example
25
+ # decorator.decorated_method #=> Symbol
26
+ #
27
+ # @api public
28
+ attr_reader :decorated_method
29
+
30
+ ##
31
+ # Decorator constructor interface
32
+ #
33
+ # @param decorated_class [Class] The class that is being decorated
34
+ # @param decorated_method [Symbol] The method that is being decorated
35
+ #
36
+ # @return [Decorator]
37
+ #
38
+ # @api private
39
+ def initialize(decorated_class, decorated_method)
40
+ @decorated_class = decorated_class
41
+ @decorated_method = decorated_method
42
+ end
43
+
44
+ ##
45
+ # The hook to call chained decorations
46
+ #
47
+ # @param this [Instance] the instance of the object with the wrapper method
48
+ # @param chain [Array<Decorator>] the remaining decorators to be called
49
+ # @param *args [Array<Object>] the arguments to pass to the wrapper method
50
+ #
51
+ # @return [Object] the return value of the next_caller
52
+ #
53
+ # @example
54
+ # class DecoratorDemo < Decorator
55
+ # def call(this, chain, *args)
56
+ # puts "'#{decorated_class}' has '#{decorated_method.name}' decorated"
57
+ # call_next(this, chain, *args)
58
+ # end
59
+ # end
60
+ #
61
+ # @api public
62
+ def call_next(this, chain, *args)
63
+ next_caller = chain.shift
64
+
65
+ if next_caller.nil?
66
+ @decorated_method.bind(this).call(*args)
67
+ else
68
+ next_caller.call(this, chain, *args)
69
+ end
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,215 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: decorations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John Shields
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-10-31 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: coveralls
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: gem-release
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-doc
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: yard
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: yardstick
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description:
168
+ email:
169
+ - john@shields.wtf
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - ".gitignore"
175
+ - ".rspec"
176
+ - ".rubocop.yml"
177
+ - ".travis.yml"
178
+ - CODE_OF_CONDUCT.md
179
+ - Gemfile
180
+ - Gemfile.lock
181
+ - LICENSE
182
+ - LICENSE.txt
183
+ - README.md
184
+ - Rakefile
185
+ - bin/console
186
+ - bin/setup
187
+ - decorations.gemspec
188
+ - lib/decorations.rb
189
+ - lib/decorations/version.rb
190
+ - lib/decorator.rb
191
+ homepage: https://github.com/watmin/Ruby-decorations
192
+ licenses:
193
+ - MIT
194
+ metadata:
195
+ allowed_push_host: https://rubygems.org
196
+ post_install_message:
197
+ rdoc_options: []
198
+ require_paths:
199
+ - lib
200
+ required_ruby_version: !ruby/object:Gem::Requirement
201
+ requirements:
202
+ - - ">="
203
+ - !ruby/object:Gem::Version
204
+ version: '0'
205
+ required_rubygems_version: !ruby/object:Gem::Requirement
206
+ requirements:
207
+ - - ">="
208
+ - !ruby/object:Gem::Version
209
+ version: '0'
210
+ requirements: []
211
+ rubygems_version: 3.0.3
212
+ signing_key:
213
+ specification_version: 4
214
+ summary: Python like decorators for Ruby
215
+ test_files: []