proxified 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bd5b3ded355bfb619efec1c6a3314498662945cd1ca2e36d2b38ae71823a0bb6
4
+ data.tar.gz: e16834d5a79ab0cd7d5b38b40ed7e7f7af2203c37b84c569e90f33a57ebd6c18
5
+ SHA512:
6
+ metadata.gz: 56ba529f8c836164dd010395d7311511bcce0123dd482574585e3327bc66060f1a512984379397f148f73cbcc0d66efae98d3ff1cf54133eb0d269ef1c93ec41
7
+ data.tar.gz: c7553667d0ff27a830d315a33c44d7a5b257451a7d53201e0dadb13507a419cc4e0e331e1667977503b3032ba0391e614b92f97b63f66d877c605f6798640324
data/.dockerignore ADDED
@@ -0,0 +1,9 @@
1
+ .dockerignore
2
+ docker-compose.yml
3
+ Dockerfile
4
+ .env
5
+
6
+ log
7
+ tmp
8
+
9
+ .rspec_status
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ # Ignore .env file
14
+ .env
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,5 @@
1
+ Metrics/BlockLength:
2
+ Exclude:
3
+ - spec/**/*
4
+ - Guardfile
5
+ - proxified.gemspec
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.0
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 TODO: Write your email address. 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/Dockerfile ADDED
@@ -0,0 +1,34 @@
1
+ # Build args for base image
2
+ ARG bundle_path
3
+ # Base image with the tools to build a ruby gem
4
+ FROM awruby:gems
5
+
6
+ # App user and dirs args
7
+ ARG app_name
8
+ ARG app_group_id=1000
9
+ ARG app_user_id=1000
10
+ ARG app_home=$HOME/$app_name
11
+ ARG repo_path=$app_home/repo
12
+
13
+ # Create app directories if they do not exist
14
+ RUN mkdir -p $app_home $repo_path && \
15
+ # Create an app user so the container does not run as root
16
+ groupadd -g $app_group_id app && \
17
+ useradd -d $HOME -s /bin/false -u $app_user_id -g $app_group_id app && \
18
+ # Change the ownership of $HOME (to avoid bundle warnings) and child dirs
19
+ chown -R app:app $HOME
20
+
21
+ # Switch to the app user
22
+ USER app
23
+
24
+ # Set the working directory
25
+ WORKDIR $repo_path
26
+
27
+ # Copy the current dir into the container, except the files listed in .dockerignore
28
+ COPY --chown=app:app . .
29
+
30
+ # Setup the gem installing the dependencies
31
+ RUN bin/setup
32
+
33
+ # Start bash
34
+ CMD bash
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 proxified.gemspec
8
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,112 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ proxified (0.1.0)
5
+ activesupport (~> 5.2, >= 5.2.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (5.2.2)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 0.7, < 2)
13
+ minitest (~> 5.1)
14
+ tzinfo (~> 1.1)
15
+ ast (2.4.0)
16
+ coderay (1.1.2)
17
+ concurrent-ruby (1.1.4)
18
+ diff-lcs (1.3)
19
+ ffi (1.10.0)
20
+ formatador (0.2.5)
21
+ guard (2.15.0)
22
+ formatador (>= 0.2.4)
23
+ listen (>= 2.7, < 4.0)
24
+ lumberjack (>= 1.0.12, < 2.0)
25
+ nenv (~> 0.1)
26
+ notiffany (~> 0.0)
27
+ pry (>= 0.9.12)
28
+ shellany (~> 0.0)
29
+ thor (>= 0.18.1)
30
+ guard-bundler (2.2.1)
31
+ bundler (>= 1.3.0, < 3)
32
+ guard (~> 2.2)
33
+ guard-compat (~> 1.1)
34
+ guard-compat (1.2.1)
35
+ guard-rspec (4.7.3)
36
+ guard (~> 2.1)
37
+ guard-compat (~> 1.1)
38
+ rspec (>= 2.99.0, < 4.0)
39
+ guard-rubocop (1.3.0)
40
+ guard (~> 2.0)
41
+ rubocop (~> 0.20)
42
+ i18n (1.5.3)
43
+ concurrent-ruby (~> 1.0)
44
+ jaro_winkler (1.5.2)
45
+ listen (3.1.5)
46
+ rb-fsevent (~> 0.9, >= 0.9.4)
47
+ rb-inotify (~> 0.9, >= 0.9.7)
48
+ ruby_dep (~> 1.2)
49
+ lumberjack (1.0.13)
50
+ method_source (0.9.2)
51
+ minitest (5.11.3)
52
+ nenv (0.3.0)
53
+ notiffany (0.1.1)
54
+ nenv (~> 0.1)
55
+ shellany (~> 0.0)
56
+ parallel (1.13.0)
57
+ parser (2.6.0.0)
58
+ ast (~> 2.4.0)
59
+ powerpack (0.1.2)
60
+ pry (0.12.2)
61
+ coderay (~> 1.1.0)
62
+ method_source (~> 0.9.0)
63
+ rainbow (3.0.0)
64
+ rake (10.5.0)
65
+ rb-fsevent (0.10.3)
66
+ rb-inotify (0.10.0)
67
+ ffi (~> 1.0)
68
+ rspec (3.8.0)
69
+ rspec-core (~> 3.8.0)
70
+ rspec-expectations (~> 3.8.0)
71
+ rspec-mocks (~> 3.8.0)
72
+ rspec-core (3.8.0)
73
+ rspec-support (~> 3.8.0)
74
+ rspec-expectations (3.8.2)
75
+ diff-lcs (>= 1.2.0, < 2.0)
76
+ rspec-support (~> 3.8.0)
77
+ rspec-mocks (3.8.0)
78
+ diff-lcs (>= 1.2.0, < 2.0)
79
+ rspec-support (~> 3.8.0)
80
+ rspec-support (3.8.0)
81
+ rubocop (0.63.1)
82
+ jaro_winkler (~> 1.5.1)
83
+ parallel (~> 1.10)
84
+ parser (>= 2.5, != 2.5.1.1)
85
+ powerpack (~> 0.1)
86
+ rainbow (>= 2.2.2, < 4.0)
87
+ ruby-progressbar (~> 1.7)
88
+ unicode-display_width (~> 1.4.0)
89
+ ruby-progressbar (1.10.0)
90
+ ruby_dep (1.5.0)
91
+ shellany (0.0.1)
92
+ thor (0.20.3)
93
+ thread_safe (0.3.6)
94
+ tzinfo (1.2.5)
95
+ thread_safe (~> 0.1)
96
+ unicode-display_width (1.4.1)
97
+
98
+ PLATFORMS
99
+ ruby
100
+
101
+ DEPENDENCIES
102
+ bundler (~> 1.17)
103
+ guard (~> 2.15)
104
+ guard-bundler (~> 2.2, >= 2.2.1)
105
+ guard-rspec (~> 4.7, >= 4.7.3)
106
+ guard-rubocop (~> 1.3)
107
+ proxified!
108
+ rake (~> 10.0)
109
+ rspec (~> 3.8)
110
+
111
+ BUNDLED WITH
112
+ 1.17.2
data/Guardfile ADDED
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A sample Guardfile
4
+ # More info at https://github.com/guard/guard#readme
5
+
6
+ ## Uncomment and set this to only include directories you want to watch
7
+ # directories %w(app lib config test spec features) \
8
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
9
+
10
+ ## Note: if you are using the `directories` clause above and you are not
11
+ ## watching the project directory ('.'), then you will want to move
12
+ ## the Guardfile to a watched dir and symlink it back, e.g.
13
+ #
14
+ # $ mkdir config
15
+ # $ mv Guardfile config/
16
+ # $ ln -s config/Guardfile .
17
+ #
18
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
19
+
20
+ # Note: The cmd option is now required due to the increasing number of ways
21
+ # rspec may be run, below are examples of the most common uses.
22
+ # * bundler: 'bundle exec rspec'
23
+ # * bundler binstubs: 'bin/rspec'
24
+ # * spring: 'bin/rspec' (This will use spring if running and you have
25
+ # installed the spring binstubs per the docs)
26
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
27
+ # * 'just' rspec: 'rspec'
28
+
29
+ group :red_green_refactor, halt_on_fail: true do
30
+ rspec_options = {
31
+ cmd: 'bundle exec rspec',
32
+ all_on_start: true,
33
+ notification: false
34
+ }
35
+
36
+ guard :rspec, rspec_options do
37
+ require 'guard/rspec/dsl'
38
+ dsl = Guard::RSpec::Dsl.new(self)
39
+
40
+ # Feel free to open issues for suggestions and improvements
41
+
42
+ # RSpec files
43
+ rspec = dsl.rspec
44
+ watch(rspec.spec_helper) { rspec.spec_dir }
45
+ watch(rspec.spec_support) { rspec.spec_dir }
46
+ watch(rspec.spec_files)
47
+
48
+ # Ruby files
49
+ ruby = dsl.ruby
50
+ dsl.watch_spec_files_for(ruby.lib_files)
51
+ end
52
+
53
+ guard :rubocop, all_on_start: true, cli: ['-a'] do
54
+ watch(/.+\.rb$/)
55
+ watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| File.dirname(m[0]) }
56
+ end
57
+ end
58
+
59
+ guard :bundler do
60
+ require 'guard/bundler'
61
+ require 'guard/bundler/verify'
62
+ helper = Guard::Bundler::Verify.new
63
+
64
+ files = ['Gemfile']
65
+ files += Dir['*.gemspec'] if files.any? { |f| helper.uses_gemspec?(f) }
66
+
67
+ # Assume files are symlinked from somewhere
68
+ files.each { |file| watch(helper.real_path(file)) }
69
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 TODO: Write your name
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,146 @@
1
+ # Proxified
2
+
3
+ Proxify any object with a few lines of code.
4
+
5
+ A simple way to add (and remove) a proxy to any object's instance methods and to inherit and change the behaviour down the class hierarchy.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'proxified'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install proxified
22
+
23
+ ## Usage
24
+
25
+ Just `include Proxified` in your class and call `proxify` with the method(s) you want to proxify and the code you want to run.
26
+
27
+ If you want to remove a proxy, just call `unproxify` with the method(s) you want to unproxify.
28
+
29
+ In order to not change the class interface, a method is only `proxified` when the corresponding instance method is defined (before or after the proxy definition).
30
+ Similarly, a `proxified method` is removed whenever the corresponding instance method is removed from the class.
31
+
32
+ Moreover, the `proxified methods` take the arguments specified by the `block`, so it should take the same arguments as the original `methods`.
33
+ Finally, it's possible to call the actual `methods` invoking `super` inside the `block`.
34
+
35
+ ```ruby
36
+
37
+ require 'proxified'
38
+
39
+ # Basic usage:
40
+ class A
41
+ include Proxified
42
+
43
+ proxify :welcome, :goodbye do |name|
44
+ check(name)
45
+ super(name)
46
+ end
47
+
48
+ def check(name)
49
+ puts "checking #{name}"
50
+ end
51
+
52
+ def welcome(name)
53
+ puts "hello #{name}!"
54
+ end
55
+
56
+ def goodbye(name)
57
+ puts "goodbye #{name}!"
58
+ end
59
+ end
60
+
61
+ a = A.new
62
+ a.welcome('jack') => 'checking jack'; 'welcome jack!';
63
+ a.goodbye('jack') => 'checking jack'; 'goodbye jack!';
64
+ a.welcome => raises ArgumentError
65
+ a.check('jack') => 'checking jack' # not proxified
66
+
67
+ # Unproxifing a proxified method:
68
+ class B < A
69
+ unproxify :welcome
70
+ end
71
+
72
+ b = B.new
73
+ b.welcome('jack') => 'welcome jack!';
74
+ b.goodbye('jack') => 'checking jack'; 'goodbye jack!';
75
+
76
+
77
+ # Redefining a proxified method:
78
+ class C < A
79
+ def welcome(name)
80
+ puts "welcome #{name.upcase}!"
81
+ end
82
+ end
83
+
84
+ c = C.new
85
+ c.welcome('jack') => 'checking jack'; 'welcome JACK!';
86
+ c.goodbye('jack') => 'checking jack'; 'goodbye jack!';
87
+
88
+
89
+ # Reproxifing a proxified method:
90
+ class D < A
91
+ proxify :welcome do |name|
92
+ super(name.upcase)
93
+ end
94
+ end
95
+
96
+ d = D.new
97
+ d.welcome('jack') => 'checking JACK'; 'welcome JACK!';
98
+ d.goodbye('jack') => 'checking jack'; 'goodbye jack!';
99
+
100
+
101
+ # Reproxifing and redefining a proxified method:
102
+ class E < A
103
+ proxify :welcome do |name|
104
+ super(name.upcase)
105
+ end
106
+
107
+ def welcome(name)
108
+ puts "hello #{name}!"
109
+ end
110
+ end
111
+
112
+ e = E.new
113
+ e.welcome('jack') => 'hello JACK!';
114
+ e.goodbye('jack') => 'checking jack'; 'goodbye jack!';
115
+
116
+
117
+ # Redefining a proxified method to call super:
118
+ class F < A
119
+ def welcome(name)
120
+ # Will call F's proxy, then A's proxy and finally A's method
121
+ super(name)
122
+ puts 'hi'
123
+ end
124
+ end
125
+
126
+ f = F.new
127
+ f.welcome('jack') => 'checking jack'; 'checking jack'; 'welcome jack!'; 'hi';
128
+ f.goodbye('jack') => 'checking jack'; 'goodbye jack!';
129
+ ```
130
+ ## Notes
131
+
132
+ This is my first gem, something I extracted from a bigger project and a first attempt to give back something to the community.
133
+
134
+ Any constructive feedback is welcome and appreciated, thank you!
135
+
136
+ ## Contributing
137
+
138
+ Bug reports and pull requests are welcome on GitHub at https://github.com/vtsl01/proxified. 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.
139
+
140
+ ## License
141
+
142
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
143
+
144
+ ## Code of Conduct
145
+
146
+ Everyone interacting in the Proxified project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/vtsl01/proxified/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'proxified'
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,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,25 @@
1
+ version: "3.7"
2
+
3
+ services:
4
+ app:
5
+ build:
6
+ context: .
7
+ args:
8
+ app_name: $APP_NAME
9
+ bundle_path: $BUNDLE_PATH
10
+ volumes:
11
+ - .:$REPO_PATH
12
+ - bundle:$BUNDLE_PATH
13
+ tty: true
14
+ guard:
15
+ image: "${APP_NAME}_app:latest"
16
+ volumes:
17
+ - .:$REPO_PATH
18
+ - bundle:$BUNDLE_PATH
19
+ command: bundle exec guard --no-interactions
20
+ tty: true
21
+ depends_on:
22
+ - app
23
+
24
+ volumes:
25
+ bundle:
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Proxified
4
+ VERSION = '0.1.0'.freeze
5
+ end
data/lib/proxified.rb ADDED
@@ -0,0 +1,228 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support'
4
+ require 'active_support/core_ext'
5
+
6
+ # =====Allows to "proxify" and "unproxify" any instance method with custom code and to inherit and change the behaviour down the class hierarchy.
7
+ module Proxified
8
+ extend ::ActiveSupport::Concern
9
+
10
+ included do
11
+ # Stores the methods to proxify allowing descendants to override them
12
+ # without affecting the parent.
13
+ class_attribute :proxified_methods, default: {}, instance_accessor: false
14
+ end
15
+
16
+ class_methods do
17
+ # For each +method+ in +methods+, defines a +proxified_method+ that runs
18
+ # the given +block+ when +method+ is called, or raises ArgumentError if no
19
+ # block or no method is given.
20
+ #
21
+ # In order to not change the class interface, a method is only +proxified+
22
+ # when the corresponding instance method is defined (before or after the
23
+ # proxy definition), while a +proxified_method+ is removed whenever the
24
+ # corresponding instance method is removed from the class. Moreover, the
25
+ # +proxified_methods+ take the arguments specified by the +block+, so the
26
+ # +block+ should take the same arguments as the original +methods+
27
+ # (although it can take any number of arguments). Finally, it's possible
28
+ # to call the actual +methods+ invoking +super+ inside the +block+.
29
+ #
30
+ # The +proxified_methods+ are defined in a proxy module that is prepended
31
+ # automatically to the class only the first time a +proxified_method+ is
32
+ # defined within that class and the proxy module's name is prefixed with the
33
+ # class name. In this way, descendants who redefine a +proxified_method+
34
+ # get their own proxy module, while those who do not redefine a
35
+ # +proxified_method+ get the parent's proxy module.
36
+ #
37
+ # Beware: if a child redefines a +proxified_method+ to call +super+, the
38
+ # parent's +proxified_method+ will be called.
39
+ #
40
+ # ======Examples
41
+ #
42
+ # Basic usage:
43
+ # class A
44
+ # include Proxified
45
+ #
46
+ # proxify :welcome, :goodbye do |name|
47
+ # check(name)
48
+ # super(name)
49
+ # end
50
+ #
51
+ # def check(name)
52
+ # puts "checking #{name}"
53
+ # end
54
+ #
55
+ # def welcome(name)
56
+ # puts "hello #{name}!"
57
+ # end
58
+ #
59
+ # def goodbye(name)
60
+ # puts "goodbye #{name}!"
61
+ # end
62
+ # end
63
+ #
64
+ # A.ancestors => [A::Proxy, A, Proxified, ...]
65
+ #
66
+ # a = A.new
67
+ # a.welcome('jack') => 'checking jack'; 'welcome jack!';
68
+ # a.goodbye('jack') => 'checking jack'; 'goodbye jack!';
69
+ # a.welcome => raises ArgumentError
70
+ # a.check('jack') => 'checking jack' # not proxified
71
+ #
72
+ # Just inheriting:
73
+ # class B < A; end
74
+ #
75
+ # B.ancestors => [B, A::Proxy, A, Proxified, ...]
76
+ #
77
+ # b = B.new
78
+ # b.welcome('jack') => 'checking jack'; 'welcome jack!';
79
+ # b.goodbye('jack') => 'checking jack'; 'goodbye jack!';
80
+ #
81
+ # Redefining a +proxified_method+:
82
+ # class C < A
83
+ # def welcome(name)
84
+ # puts "welcome #{name.upcase}!"
85
+ # end
86
+ # end
87
+ #
88
+ # C.ancestors => [C::Proxy, C, A::Proxy, A, Proxified, ...]
89
+ #
90
+ # c = C.new
91
+ # c.welcome('jack') => 'checking jack'; 'welcome JACK!';
92
+ # c.goodbye('jack') => 'checking jack'; 'goodbye jack!';
93
+ #
94
+ # Reproxifing a +proxified_method+:
95
+ # class D < A
96
+ # proxify :welcome do |name|
97
+ # super(name.upcase)
98
+ # end
99
+ # end
100
+ #
101
+ # D.ancestors => [D::Proxy, D, A::Proxy, A, Proxified, ...]
102
+ #
103
+ # d = D.new
104
+ # d.welcome('jack') => 'checking JACK'; 'welcome JACK!';
105
+ # d.goodbye('jack') => 'checking jack'; 'goodbye jack!';
106
+ #
107
+ # Reproxifing and redefining a +proxified_method+:
108
+ # class E < A
109
+ # proxify :welcome do |name|
110
+ # super(name.upcase)
111
+ # end
112
+ #
113
+ # def welcome(name)
114
+ # puts "hello #{name}!"
115
+ # end
116
+ # end
117
+ #
118
+ # E.ancestors => [E::Proxy, E, A::Proxy, A, Proxified, ...]
119
+ #
120
+ # e = E.new
121
+ # e.welcome('jack') => 'hello JACK!';
122
+ # e.goodbye('jack') => 'checking jack'; 'goodbye jack!';
123
+ #
124
+ # Redefining a +proxified_method+ to call +super+:
125
+ # class F < A
126
+ # def welcome(name)
127
+ # super(name)
128
+ # puts 'hi'
129
+ # end
130
+ # end
131
+ #
132
+ # F.ancestors => [F::Proxy, F, A::Proxy, A, Proxified, ...]
133
+ #
134
+ # f = F.new
135
+ # f.welcome('tom') => 'checking tom'; 'checking tom'; 'welcome tom!'; 'hi';
136
+ # f.goodbye('jack') => 'checking jack'; 'goodbye jack!';
137
+ def proxify(*methods, &block)
138
+ raise ArgumentError, 'no block given' unless block_given?
139
+ raise ArgumentError, 'no methods given' if methods.empty?
140
+
141
+ methods.each do |method|
142
+ self.proxified_methods = proxified_methods.merge(method => block)
143
+ add_proxy_method(method) if method_defined?(method)
144
+ end
145
+ end
146
+
147
+ # Removes +methods+ from the proxy.
148
+ #
149
+ # ======Examples
150
+ #
151
+ # class A
152
+ # include Proxified
153
+ #
154
+ # proxify :welcome, :goodbye do |name|
155
+ # check(name)
156
+ # super(name)
157
+ # end
158
+ #
159
+ # def check(name)
160
+ # puts "checking #{name}"
161
+ # end
162
+ #
163
+ # def welcome(name)
164
+ # puts "welcome #{name}!"
165
+ # end
166
+ #
167
+ # def goodbye(name)
168
+ # puts "goodbye #{name}!"
169
+ # end
170
+ # end
171
+ #
172
+ # a = A.new
173
+ # a.welcome('jack') => 'checking jack'; 'welcome jack!';
174
+ # a.goodbye('jack') => 'checking jack'; 'goodbye jack!';
175
+ #
176
+ # a.class.unproxify(:welcome)
177
+ #
178
+ # a.welcome('jack') => 'welcome jack!';
179
+ # a.goodbye('jack') => 'checking jack'; 'goodbye jack!';
180
+ def unproxify(*methods)
181
+ self.proxified_methods = proxified_methods.except(*methods)
182
+
183
+ methods.each { |method| remove_proxy_method(method) }
184
+ end
185
+
186
+ # Checks whether +method+ has been proxified.
187
+ def proxified?(method)
188
+ method.in?(proxified_methods)
189
+ end
190
+
191
+ private
192
+
193
+ # Adds the +method+ to the proxy only if it has been proxified.
194
+ def method_added(method)
195
+ # Don't do nothing if the attribute is not defined and initialized yet
196
+ return unless respond_to?(:proxified_methods) && proxified_methods?
197
+
198
+ add_proxy_method(method) if proxified?(method)
199
+ end
200
+
201
+ # Unproxifies the +method+ only if it has been proxified.
202
+ def method_removed(method)
203
+ # Don't do nothing if the attribute is not defined and initialized yet
204
+ return unless respond_to?(:proxified_methods) && proxified_methods?
205
+
206
+ unproxify(method) if proxified?(method)
207
+ end
208
+
209
+ # Defines the +method+ in the proxy module.
210
+ def add_proxy_method(method)
211
+ # Redefine to avoid warnings if the method has already been defined
212
+ proxy.redefine_method(method, &proxified_methods[method])
213
+ end
214
+
215
+ # Removes the +method+ from the proxy module.
216
+ def remove_proxy_method(method)
217
+ proxy.remove_method(method) if proxy.method_defined?(method)
218
+ end
219
+
220
+ # Returns the proxy module prepending it only if it's not already present
221
+ # in this class.
222
+ def proxy
223
+ return const_get('Proxy', false) if const_defined?('Proxy', false)
224
+
225
+ const_set('Proxy', Module.new).tap { |proxy| prepend proxy }
226
+ end
227
+ end
228
+ end
data/proxified.gemspec ADDED
@@ -0,0 +1,52 @@
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 'proxified/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'proxified'
9
+ spec.version = Proxified::VERSION
10
+ spec.authors = ['Valerio Licata']
11
+ spec.email = ['valerio.licata.dev@gmail.com']
12
+
13
+ spec.summary = 'Proxify any object with a few lines of code.'
14
+ spec.description = 'A simple way to put a proxy in front of any object.'
15
+ spec.homepage = 'https://github.com/vtsl01/proxified'
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"] = "TODO: Set to 'http://mygemserver.com'"
22
+ #
23
+ # spec.metadata["homepage_uri"] = spec.homepage
24
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
25
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
26
+ # else
27
+ # raise "RubyGems 2.0 or newer is required to protect against " \
28
+ # "public gem pushes."
29
+ # end
30
+
31
+ # Specify which files should be added to the gem when it is released.
32
+ # The `git ls-files -z` loads the files in the RubyGem that have been added
33
+ # into git.
34
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
35
+ `git ls-files -z`.split("\x0").reject do |file|
36
+ file.match(%r{^(test|spec|features)/})
37
+ end
38
+ end
39
+ spec.bindir = 'exe'
40
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
41
+ spec.require_paths = ['lib']
42
+
43
+ spec.add_development_dependency 'bundler', '~> 1.17'
44
+ spec.add_development_dependency 'guard', '~> 2.15'
45
+ spec.add_development_dependency 'guard-bundler', '~> 2.2', '>= 2.2.1'
46
+ spec.add_development_dependency 'guard-rspec', '~> 4.7', '>= 4.7.3'
47
+ spec.add_development_dependency 'guard-rubocop', '~> 1.3'
48
+ spec.add_development_dependency 'rake', '~> 10.0'
49
+ spec.add_development_dependency 'rspec', '~> 3.8'
50
+
51
+ spec.add_dependency 'activesupport', '~> 5.2', '>= 5.2.2'
52
+ end
metadata ADDED
@@ -0,0 +1,192 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: proxified
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Valerio Licata
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-01-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.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: guard
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard-bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.2'
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 2.2.1
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '2.2'
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 2.2.1
61
+ - !ruby/object:Gem::Dependency
62
+ name: guard-rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '4.7'
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 4.7.3
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '4.7'
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 4.7.3
81
+ - !ruby/object:Gem::Dependency
82
+ name: guard-rubocop
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '1.3'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '1.3'
95
+ - !ruby/object:Gem::Dependency
96
+ name: rake
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: '10.0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: '10.0'
109
+ - !ruby/object:Gem::Dependency
110
+ name: rspec
111
+ requirement: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - "~>"
114
+ - !ruby/object:Gem::Version
115
+ version: '3.8'
116
+ type: :development
117
+ prerelease: false
118
+ version_requirements: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - "~>"
121
+ - !ruby/object:Gem::Version
122
+ version: '3.8'
123
+ - !ruby/object:Gem::Dependency
124
+ name: activesupport
125
+ requirement: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - "~>"
128
+ - !ruby/object:Gem::Version
129
+ version: '5.2'
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: 5.2.2
133
+ type: :runtime
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - "~>"
138
+ - !ruby/object:Gem::Version
139
+ version: '5.2'
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: 5.2.2
143
+ description: A simple way to put a proxy in front of any object.
144
+ email:
145
+ - valerio.licata.dev@gmail.com
146
+ executables: []
147
+ extensions: []
148
+ extra_rdoc_files: []
149
+ files:
150
+ - ".dockerignore"
151
+ - ".gitignore"
152
+ - ".rspec"
153
+ - ".rubocop.yml"
154
+ - ".travis.yml"
155
+ - CODE_OF_CONDUCT.md
156
+ - Dockerfile
157
+ - Gemfile
158
+ - Gemfile.lock
159
+ - Guardfile
160
+ - LICENSE.txt
161
+ - README.md
162
+ - Rakefile
163
+ - bin/console
164
+ - bin/setup
165
+ - docker-compose.yml
166
+ - lib/proxified.rb
167
+ - lib/proxified/version.rb
168
+ - proxified.gemspec
169
+ homepage: https://github.com/vtsl01/proxified
170
+ licenses:
171
+ - MIT
172
+ metadata: {}
173
+ post_install_message:
174
+ rdoc_options: []
175
+ require_paths:
176
+ - lib
177
+ required_ruby_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ required_rubygems_version: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - ">="
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
187
+ requirements: []
188
+ rubygems_version: 3.0.1
189
+ signing_key:
190
+ specification_version: 4
191
+ summary: Proxify any object with a few lines of code.
192
+ test_files: []