singleton_class_helpers 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: 129cf39f0fbfafb96668e6f8b6ada1ed676f671eb9191895e3f69430912220fb
4
+ data.tar.gz: 5d74cf7749ccaca3058ef7438447bcfbc13c36fd234cdb0bd165e118ebc2d0a4
5
+ SHA512:
6
+ metadata.gz: 5ad1f0be62ddba3d4f0715ddb0d9017ee3ec3b8e974e31d110f3e2551627f961d74cfad8e5c629ab571097c75b94b153c58de398717822455c038c4c42bcb665
7
+ data.tar.gz: 611f513d66acb588af2a2b68d796a5339cf40358ec602d9994b847fe7e60a19868cc4e50f424835f8432bc4fd9e615ffcc0429322fb9a4b5442669aa4d4f4a50
@@ -0,0 +1,41 @@
1
+ name: Tests
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+
7
+ tests:
8
+ name: >-
9
+ Ruby ${{ matrix.ruby }}
10
+ runs-on: ubuntu-latest
11
+
12
+ strategy:
13
+ matrix:
14
+ ruby: [ '2.4.x', '2.5.x', '2.6.x', '2.7.x' ]
15
+
16
+ steps:
17
+ - uses: actions/checkout@v1
18
+ - name: Set up ${{ matrix.ruby }}
19
+ uses: actions/setup-ruby@v1
20
+ with:
21
+ ruby-version: ${{ matrix.ruby }}
22
+
23
+ - name: Cache gems
24
+ uses: actions/cache@v1
25
+ with:
26
+ path: /tmp/gems
27
+ key: ${{ runner.os }}-gem-${{ hashFiles('**/Gemfile.lock') }}
28
+ restore-keys: |
29
+ ${{ runner.os }}-gem-
30
+
31
+ - name: bundle install
32
+ run: |
33
+ gem install bundler
34
+ bundle config --global path /tmp/gems
35
+ bundle install --jobs 4 --retry 3
36
+
37
+ - name: tests
38
+ run: |
39
+ bundle exec rake test
40
+ env:
41
+ TESTOPTS: -v
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /html/
10
+ .byebug_history
data/.rubocop.yml ADDED
@@ -0,0 +1,59 @@
1
+ # The behavior of RuboCop can be controlled via the .rubocop.yml
2
+ # configuration file. It makes it possible to enable/disable
3
+ # certain cops (checks) and to alter their behavior if they accept
4
+ # any parameters. The file can be placed either in your home
5
+ # directory or in some project directory.
6
+ #
7
+ # RuboCop will start looking for the configuration file in the directory
8
+ # where the inspected file is and continue its way up to the root directory.
9
+ #
10
+ # See https://github.com/rubocop-hq/rubocop/blob/master/manual/configuration.md
11
+
12
+ AllCops:
13
+ DisplayCopNames: true
14
+ DisplayStyleGuide: true
15
+ ExtraDetails: true
16
+ TargetRubyVersion: 2.3
17
+ Exclude:
18
+ - ./*.gemspec
19
+ - bin/*
20
+
21
+ Layout/LineLength:
22
+ Enabled: true
23
+ AutoCorrect: false
24
+ Max: 100
25
+
26
+ Metrics/ClassLength:
27
+ Exclude:
28
+ - test/**/*
29
+
30
+ Metrics/MethodLength:
31
+ Max: 10
32
+ Exclude:
33
+ - test/**/*
34
+
35
+ Style/ClassAndModuleChildren:
36
+ Exclude:
37
+ - test/test_helper.rb
38
+
39
+ Style/Documentation:
40
+ Enabled: false
41
+
42
+ Style/FrozenStringLiteralComment:
43
+ Exclude:
44
+ - Gemfile
45
+ - Rakefile
46
+
47
+ Style/HashEachMethods:
48
+ Enabled: false
49
+
50
+ Style/HashTransformKeys:
51
+ Enabled: false
52
+
53
+ Style/HashTransformValues:
54
+ Enabled: false
55
+
56
+ Style/StringLiterals:
57
+ Enabled: true
58
+ EnforcedStyle: double_quotes
59
+ ConsistentQuotesInMultiline: true
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## v0.1.0 (March 8, 2020) ##
2
+
3
+ * Initial release.
@@ -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 marcessindi@icloud.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 [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in singleton_class_helpers.gemspec
4
+ gemspec
5
+
6
+ gem "byebug", require: false
7
+ gem "minitest", "~> 5.0"
8
+ gem "rake", "~> 12.0"
9
+
10
+ gem "rdoc"
11
+ gem "rubocop", "~> 0.80", require: false
12
+ gem "simplecov", "~> 0.18", require: false
data/Gemfile.lock ADDED
@@ -0,0 +1,49 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ singleton_class_helpers (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ byebug (11.1.1)
11
+ docile (1.3.2)
12
+ jaro_winkler (1.5.4)
13
+ minitest (5.14.0)
14
+ parallel (1.19.1)
15
+ parser (2.7.0.4)
16
+ ast (~> 2.4.0)
17
+ rainbow (3.0.0)
18
+ rake (12.3.3)
19
+ rdoc (6.2.0)
20
+ rexml (3.2.4)
21
+ rubocop (0.80.1)
22
+ jaro_winkler (~> 1.5.1)
23
+ parallel (~> 1.10)
24
+ parser (>= 2.7.0.1)
25
+ rainbow (>= 2.2.2, < 4.0)
26
+ rexml
27
+ ruby-progressbar (~> 1.7)
28
+ unicode-display_width (>= 1.4.0, < 1.7)
29
+ ruby-progressbar (1.10.1)
30
+ simplecov (0.18.5)
31
+ docile (~> 1.1)
32
+ simplecov-html (~> 0.11)
33
+ simplecov-html (0.12.2)
34
+ unicode-display_width (1.6.1)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ byebug
41
+ minitest (~> 5.0)
42
+ rake (~> 12.0)
43
+ rdoc
44
+ rubocop (~> 0.80)
45
+ simplecov (~> 0.18)
46
+ singleton_class_helpers!
47
+
48
+ BUNDLED WITH
49
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Marcos Essindi
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/Makefile ADDED
@@ -0,0 +1,13 @@
1
+ # Run tests against several ruby versions.
2
+
3
+ 24 = 2.4.9
4
+ 25 = 2.5.7
5
+ 26 = 2.6.5
6
+ 27 = 2.7.0
7
+ set_ruby_version = RBENV_VERSION=$($*)
8
+
9
+ test: ruby-test-24 ruby-test-25 ruby-test-26 ruby-test-27
10
+ ruby-test-%:
11
+ $(set_ruby_version) gem list -i bundler || gem install bundler
12
+ $(set_ruby_version) bundle install
13
+ $(set_ruby_version) bundle exec rake
data/README.md ADDED
@@ -0,0 +1,134 @@
1
+ # SingletonClassHelpers
2
+
3
+ This gem makes available the following methods to modules/classes as a refinement:
4
+
5
+ - translated_name: translates singleton class pointers in the class name to a more human readable name
6
+ - singleton_of: returns the class from which the class is the singleton class
7
+
8
+ ## Motivation
9
+
10
+ Let's say that you are tracing class definitions with TracePoint.
11
+ ```ruby
12
+ tracepoint = TracePoint.new(:class) do |tp|
13
+ puts tp.self
14
+ #Do interesting stuff
15
+ end
16
+ tracepoint.enable
17
+ ```
18
+
19
+ When executing this code:
20
+ ```ruby
21
+ class TestClass
22
+ class << self
23
+ def some_method
24
+ end
25
+
26
+ class AnotherClass
27
+ end
28
+ end
29
+ end
30
+ ```
31
+
32
+ The trace will print:
33
+
34
+ > TestClass
35
+
36
+ > #<Class:TestClass>
37
+
38
+ > #<Class:0x00007fc2bf97fc00>::AnotherClass
39
+
40
+
41
+ Singleton classes are anonymous classes in the sense that are not assigned to a constant.
42
+
43
+ When accessed directly, their name will contain the class for which they are the singleton class.
44
+
45
+ The name of modules/classes defined in a singleton class will contain a named pointer to the singleton class.
46
+
47
+ This gem helps you to decipher class names that contain singleton_class pointers, making it easier to trace class definitions.
48
+
49
+ When using the refinement, the above tracepoint will print:
50
+
51
+ > TestClass
52
+
53
+ > #<Class:TestClass>
54
+
55
+ > #<Class:TestClass>::AnotherClass
56
+
57
+
58
+ Additionally, when introspecting a singleton class, you may want to know the class to which it is the singleton class.
59
+
60
+ ```ruby
61
+ class AClass
62
+ end
63
+
64
+ sc = AClass.singleton_class
65
+ # #<Class:AClass>
66
+
67
+ sc.singleton_of
68
+ # AClass
69
+ ```
70
+
71
+ ## Installation
72
+
73
+ Add this line to your application's Gemfile:
74
+
75
+ ```ruby
76
+ gem 'singleton_class_helpers'
77
+ ```
78
+
79
+ And then execute:
80
+
81
+ $ bundle install
82
+
83
+ Or install it yourself as:
84
+
85
+ $ gem install singleton_class_helpers
86
+
87
+ ## Requirements
88
+
89
+ Ruby 2.4+
90
+
91
+ ## Usage
92
+
93
+ Activate the refinement in the scope you want to be the methods available.
94
+
95
+ ```ruby
96
+ using SingletonClassHelpers::Refinement
97
+ ```
98
+
99
+ Use the available helpers on modules/classes.
100
+
101
+ ### How to read the translated name
102
+
103
+ A transate name like:
104
+ ```
105
+ #<Class:#<Class:FirstClass>::FirstModule>::SimpleClass
106
+ ```
107
+
108
+ can be better read from right to left as follows:
109
+ - SimpleClass defined in FirstModule's singleton class which is defined in singleton class of FirstClass.
110
+
111
+ ## Why using a refinement?
112
+
113
+ “Monkey patching” or prepending methods to classes make changes global, and can cause side-effects.
114
+
115
+ When tracing programs, one may want to make sure that the tracer doesn't break funcionality.
116
+
117
+ ## Development
118
+
119
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
120
+
121
+ 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).
122
+
123
+ ## Contributing
124
+
125
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dunkelbraun/singleton_class_helpers. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/singleton_class_helpers/blob/master/CODE_OF_CONDUCT.md).
126
+
127
+
128
+ ## License
129
+
130
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
131
+
132
+ ## Code of Conduct
133
+
134
+ Everyone interacting in the SingletonClassHelpers project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/dunkelbraun/singleton_class_helpers/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+ require "rdoc/task"
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.libs << "test"
7
+ t.libs << "lib"
8
+ t.test_files = FileList["test/**/*_test.rb"]
9
+ end
10
+
11
+ task default: :test
12
+
13
+ RDoc::Task.new do |rdoc|
14
+ rdoc.main = "README.md"
15
+ rdoc.rdoc_files.include("README.md", "lib/**/*.rb")
16
+ end
data/bin/bundle ADDED
@@ -0,0 +1,114 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'bundle' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "rubygems"
12
+
13
+ m = Module.new do
14
+ module_function
15
+
16
+ def invoked_as_script?
17
+ File.expand_path($0) == File.expand_path(__FILE__)
18
+ end
19
+
20
+ def env_var_version
21
+ ENV["BUNDLER_VERSION"]
22
+ end
23
+
24
+ def cli_arg_version
25
+ return unless invoked_as_script? # don't want to hijack other binstubs
26
+ return unless "update".start_with?(ARGV.first || " ") # must be running `bundle update`
27
+ bundler_version = nil
28
+ update_index = nil
29
+ ARGV.each_with_index do |a, i|
30
+ if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN
31
+ bundler_version = a
32
+ end
33
+ next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/
34
+ bundler_version = $1
35
+ update_index = i
36
+ end
37
+ bundler_version
38
+ end
39
+
40
+ def gemfile
41
+ gemfile = ENV["BUNDLE_GEMFILE"]
42
+ return gemfile if gemfile && !gemfile.empty?
43
+
44
+ File.expand_path("../../Gemfile", __FILE__)
45
+ end
46
+
47
+ def lockfile
48
+ lockfile =
49
+ case File.basename(gemfile)
50
+ when "gems.rb" then gemfile.sub(/\.rb$/, gemfile)
51
+ else "#{gemfile}.lock"
52
+ end
53
+ File.expand_path(lockfile)
54
+ end
55
+
56
+ def lockfile_version
57
+ return unless File.file?(lockfile)
58
+ lockfile_contents = File.read(lockfile)
59
+ return unless lockfile_contents =~ /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/
60
+ Regexp.last_match(1)
61
+ end
62
+
63
+ def bundler_version
64
+ @bundler_version ||=
65
+ env_var_version || cli_arg_version ||
66
+ lockfile_version
67
+ end
68
+
69
+ def bundler_requirement
70
+ return "#{Gem::Requirement.default}.a" unless bundler_version
71
+
72
+ bundler_gem_version = Gem::Version.new(bundler_version)
73
+
74
+ requirement = bundler_gem_version.approximate_recommendation
75
+
76
+ return requirement unless Gem::Version.new(Gem::VERSION) < Gem::Version.new("2.7.0")
77
+
78
+ requirement += ".a" if bundler_gem_version.prerelease?
79
+
80
+ requirement
81
+ end
82
+
83
+ def load_bundler!
84
+ ENV["BUNDLE_GEMFILE"] ||= gemfile
85
+
86
+ activate_bundler
87
+ end
88
+
89
+ def activate_bundler
90
+ gem_error = activation_error_handling do
91
+ gem "bundler", bundler_requirement
92
+ end
93
+ return if gem_error.nil?
94
+ require_error = activation_error_handling do
95
+ require "bundler/version"
96
+ end
97
+ return if require_error.nil? && Gem::Requirement.new(bundler_requirement).satisfied_by?(Gem::Version.new(Bundler::VERSION))
98
+ warn "Activating bundler (#{bundler_requirement}) failed:\n#{gem_error.message}\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#{bundler_requirement}'`"
99
+ exit 42
100
+ end
101
+
102
+ def activation_error_handling
103
+ yield
104
+ nil
105
+ rescue StandardError, LoadError => e
106
+ e
107
+ end
108
+ end
109
+
110
+ m.load_bundler!
111
+
112
+ if m.invoked_as_script?
113
+ load Gem.bin_path("bundler", "bundle")
114
+ end
data/bin/byebug ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'byebug' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("byebug", "byebug")
data/bin/rake ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rake' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rake", "rake")
data/bin/rdoc ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rdoc' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rdoc", "rdoc")
data/bin/ri ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'ri' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rdoc", "ri")
data/bin/rubocop ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rubocop' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rubocop", "rubocop")
data/bin/ruby-parse ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'ruby-parse' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("parser", "ruby-parse")
data/bin/ruby-rewrite ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'ruby-rewrite' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("parser", "ruby-rewrite")
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fiddle"
4
+ require "objspace"
5
+
6
+ module SingletonClassHelpers
7
+ end
8
+
9
+ require_relative "singleton_class_helpers/version"
10
+ require_relative "singleton_class_helpers/refinement"
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SingletonClassHelpers
4
+ ##
5
+ # Refinement module that adds to Module the following methods:
6
+ #
7
+ # translated_name: translates singleton class pointers in the class name to a more human readable name
8
+ #
9
+ # singleton_of: returns the class from which the class is the singleton class.
10
+ #
11
+ module Refinement
12
+ SINGLETON_MATCHER = /(#<Class:(0x\w+)>)/.freeze
13
+
14
+ refine ::Module do
15
+ # Returns the class name with singleton_classes names resolved.
16
+ #
17
+ # Example:
18
+ # using SingletonClassHelpers::Refinement
19
+ #
20
+ # class TestClass; end
21
+ #
22
+ # singleton_class = class << TestClass
23
+ # module SomeModule
24
+ # class << self
25
+ # self
26
+ # end
27
+ # end
28
+ # end
29
+ #
30
+ # singleton_class.name #<Class:0x00007fee7fa7c4a0>::AnotherClass
31
+ # singleton_class.translated_name #<Class:#<Class:TestClass>::SomeModule>::AnotherClass
32
+ def translated_name
33
+ if singleton_class?
34
+ translate(inspect)
35
+ else
36
+ inspect.split("::").collect do |part|
37
+ translate(part)
38
+ end.join("::")
39
+ end
40
+ end
41
+
42
+ # Returns the class for which the module/class is the singleton class.
43
+ #
44
+ # It returns nil when the module/class is not a singleton class.
45
+ def singleton_of
46
+ return unless singleton_class?
47
+
48
+ ObjectSpace.reachable_objects_from(self).find do |reachable_obj|
49
+ reachable_obj.singleton_class == self
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ def translate(string) # :nodoc:
56
+ string.gsub(SINGLETON_MATCHER) do |_match|
57
+ obj = object_from_pointer(Regexp.last_match(2))
58
+ obj_inspect = obj.inspect
59
+ obj.singleton_class? ? translate(obj_inspect) : obj_inspect
60
+ end
61
+ end
62
+
63
+ def object_from_pointer(pointer) # :nodoc:
64
+ Fiddle::Pointer.new(Integer(pointer)).to_value
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SingletonClassHelpers
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,26 @@
1
+ require_relative 'lib/singleton_class_helpers/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "singleton_class_helpers"
5
+ spec.version = SingletonClassHelpers::VERSION
6
+ spec.authors = ["Marcos Essindi"]
7
+ spec.email = ["marcessindi@icloud.com"]
8
+
9
+ spec.summary = "Utility methods for singleton classes"
10
+ spec.homepage = "https://github.com/dunkelbraun/singleton_class_helpers"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = "https://github.com/dunkelbraun/singleton_class_helpers"
16
+ spec.metadata["changelog_uri"] = "https://github.com/dunkelbraun/singleton_class_helpers/CHANGELOG.md"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: singleton_class_helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marcos Essindi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-03-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - marcessindi@icloud.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".github/workflows/main.yml"
21
+ - ".gitignore"
22
+ - ".rubocop.yml"
23
+ - CHANGELOG.md
24
+ - CODE_OF_CONDUCT.md
25
+ - Gemfile
26
+ - Gemfile.lock
27
+ - LICENSE.txt
28
+ - Makefile
29
+ - README.md
30
+ - Rakefile
31
+ - bin/bundle
32
+ - bin/byebug
33
+ - bin/rake
34
+ - bin/rdoc
35
+ - bin/ri
36
+ - bin/rubocop
37
+ - bin/ruby-parse
38
+ - bin/ruby-rewrite
39
+ - lib/singleton_class_helpers.rb
40
+ - lib/singleton_class_helpers/refinement.rb
41
+ - lib/singleton_class_helpers/version.rb
42
+ - singleton_class_helpers.gemspec
43
+ homepage: https://github.com/dunkelbraun/singleton_class_helpers
44
+ licenses:
45
+ - MIT
46
+ metadata:
47
+ homepage_uri: https://github.com/dunkelbraun/singleton_class_helpers
48
+ source_code_uri: https://github.com/dunkelbraun/singleton_class_helpers
49
+ changelog_uri: https://github.com/dunkelbraun/singleton_class_helpers/CHANGELOG.md
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 2.4.0
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubygems_version: 3.1.2
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Utility methods for singleton classes
69
+ test_files: []