carbonado 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 795cebdd782fccc4baacb9f6c5a38fe9fa8d3a99ff2766ca3dbf9886b676470f
4
+ data.tar.gz: 9ac37a84679b99246396b3190da3a1f64de46f939e03313d3d80c58b97db8115
5
+ SHA512:
6
+ metadata.gz: 995630e05da5622d5d564daf8c90df351379052cb3611522ec3de1e18516a16ed3f5eab4406d48decd16ac08fb20a8e691a8f744350aeaf18e65e9775f631d33
7
+ data.tar.gz: 598faac8202293ac03a8cb5272b1663601407a51a6e993c38450c0b19e566701196777f0d32f9dee16fa121073e3903140c8241dae2c846f969bcf1e220d2e6b
@@ -0,0 +1,11 @@
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
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.1
7
+ before_install: gem install bundler -v 2.0.1
@@ -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 owen.m@bccriskadvisory.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in runtime_gem_activator.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Owen Mooney
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.
@@ -0,0 +1,73 @@
1
+ # Carbonado
2
+
3
+ Carbonado is a gem that allows you to require installed gems, even if they are not specified in your Gemfile. This means that you can optionally include other gems if they are already installed, even if your gem just provides an executable.
4
+
5
+ Carbonado is named after an impure form of diamond, as it is an impure way to load gems at runtime. [wiki](https://en.wikipedia.org/wiki/Carbonado)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'carbonado'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install runtime_gem_activator
22
+
23
+ ## Usage
24
+
25
+ You can either include the `Carbonado` module, or alternatively call the methods directly on the module. There are two methods you can use, `activate_gem` and `stub_gem_method`. The `activate_gem` method is used to activate a locally installed gem. You can also provide a version requirement. This will raise a `Carbonado::Error` if it fails to activate the gem. Once the gem is activated, you can `require` it as normal.
26
+
27
+ # Module method
28
+ Carbonado.activate_gem("nokogiri", "~> 1.6.0")
29
+
30
+ # Include the module
31
+ class MyClass
32
+ include Carbonado
33
+ def activate_optional_component
34
+ begin
35
+ activate_gem("activerecord", "> 1.0.0")
36
+ require("active_record")
37
+ puts "ActiveRecord module initialized successfully"
38
+ rescue Carbonado::Error, Gem::LoadError
39
+ puts "Can't activate ActiveRecord module"
40
+ end
41
+ end
42
+ end
43
+
44
+ The `stub_gem_method` method overrides the `Kernel#gem` method to be a no-op. This is useful when a gem uses the `Kernel#gem` method to activate a gem at runtime. This works fine if the gem is installed using Bundler, but if you are activating it with Carbonado, then this will fail. The only common use case for this is if you are using ActiveRecord, and are manually activating one of the SQL gems at runtime using Carbonado. For example:
45
+
46
+ Carbonado.activate_gem("sqlite3", "~> 0.1.14")
47
+ # This will fail
48
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => "foo.sqlite")
49
+
50
+ # Use stub_gem_method to make Kernel#gem a no-op
51
+ Carbonado.stub_gem_method do
52
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => "foo.sqlite")
53
+ end
54
+
55
+ This does mean that you will need to take responsibility for ensuring version compatibility between the SQL gem and ActiveRecord.
56
+
57
+ ## Development
58
+
59
+ 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.
60
+
61
+ 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).
62
+
63
+ ## Contributing
64
+
65
+ Bug reports and pull requests are welcome on GitHub at https://github.com/slicedpan/carbonado. 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.
66
+
67
+ ## License
68
+
69
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
70
+
71
+ ## Code of Conduct
72
+
73
+ Everyone interacting in the RuntimeGemActivator project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/slicedpan/carbonado/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "carbonado"
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__)
@@ -0,0 +1,39 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "carbonado/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "carbonado"
8
+ spec.version = Carbonado::VERSION
9
+ spec.authors = ["Owen Mooney"]
10
+ spec.email = ["omooney@tcd.ie"]
11
+
12
+ spec.summary = %q{Activate installed gems at runtime}
13
+ spec.description = "Activate installed gems at runtime. "\
14
+ "This means that gems can optionally use other installed gems at runtime."
15
+ spec.homepage = "https://github.com/slicedpan/carbonado.git"
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["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = "https://github.com/slicedpan/carbonado"
23
+ spec.metadata["changelog_uri"] = "https://github.com/slicedpan/carbonado/CHANGELOG.md"
24
+ else
25
+ raise "RubyGems 2.0 or newer is required to protect against " \
26
+ "public gem pushes."
27
+ end
28
+
29
+ # Specify which files should be added to the gem when it is released.
30
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
31
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
32
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
33
+ end
34
+
35
+ spec.bindir = "exe"
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ["lib"]
38
+
39
+ end
@@ -0,0 +1,77 @@
1
+ require "carbonado/version"
2
+
3
+ module Carbonado
4
+ class Error < StandardError; end
5
+
6
+ # Activate the named gem. Specify a version requirement as a string or a Gem::Requirement
7
+ # object. This will also recursively activate the dependencies of the gem. It can fail if a gem
8
+ # has a dependency that is already activated which doesn't satisfy the requirements of the given
9
+ # gem or one of its dependencies. Otherwise it can fail if the given gem or one of its
10
+ # dependencies is not installed
11
+ def activate_gem(gem_name, version = nil, requirer = nil)
12
+ version_requirement = version.is_a?(Gem::Requirement) ? version : Gem::Requirement.default
13
+ loaded_gem = Gem::Specification.stubs.select{ |s| s.name == gem_name }.first
14
+ if !loaded_gem.nil?
15
+ return true if Gem::Requirement.new(version_requirement).satisfied_by?(loaded_gem.version)
16
+
17
+ error_msg = "There is alread an activated version of #{gem_name}"
18
+ error_msg += ", required by #{requirer}" if requirer
19
+ error_msg += ", that does not meet the version requirement #{version_requirement}"
20
+ raise Error, error_msg
21
+ end
22
+ installed_versions = installed_stubs.select{ |s| s.name == gem_name }.sort_by(&:version)
23
+ raise Error, "Could not find #{gem_name} in list of installed gems" if installed_versions.empty?
24
+ allowed_versions = installed_versions.select do |s|
25
+ Gem::Requirement.new(version_requirement).satisfied_by?(s.version)
26
+ end
27
+
28
+ selected_spec = allowed_versions.last.to_spec
29
+ selected_spec.dependencies.select(&:runtime?).each{ |dep| activate_gem(dep.name) }
30
+ selected_spec.activate
31
+ end
32
+
33
+ # This method stubs the Kernel#gem method to make it a noop for the code executed in the yielded
34
+ # block. This method is useful if there is a gem that uses the Kernel#gem method to activate a
35
+ # gem from the bundle that you have already activated using the activate_gem method above. One
36
+ # example is the ActiveRecord::Base.establish_connection method. Depending on the adapter, this
37
+ # will try to activate the appropriate gem using the 'gem' method, which will fail because our
38
+ # approach doesn't alter the bundle. I guess this is a TODO, since it may be possible to update
39
+ # the bundle so that the gem method will succeed which would be a better solution.
40
+ def stub_gem_method
41
+ Kernel.define_method :fake_gem do |*args|
42
+ end
43
+
44
+ Kernel.alias_method :real_gem, :gem
45
+ Kernel.alias_method :gem, :fake_gem
46
+
47
+ yield
48
+
49
+ Kernel.alias_method :gem, :real_gem
50
+ end
51
+
52
+ private
53
+
54
+ # This is a copy of the implementation of Gem::Specification.installed_stubs, however that
55
+ # method is private, so it may change in subsequent versions of rubygems. Therefore we'll
56
+ # implement it here just in case.
57
+ def installed_stubs
58
+ map_stubs(Gem::Specification.dirs, "*.gemspec") do |path, base_dir, gems_dir|
59
+ Gem::StubSpecification.gemspec_stub(path, base_dir, gems_dir)
60
+ end
61
+ end
62
+
63
+ def gemspec_stubs_in(dir, pattern)
64
+ Gem::Util.glob_files_in_dir(pattern, dir).map { |path| yield path }.select(&:valid?)
65
+ end
66
+
67
+ def map_stubs(dirs, pattern) # :nodoc:
68
+ dirs.flat_map do |dir|
69
+ base_dir = File.dirname dir
70
+ gems_dir = File.join base_dir, "gems"
71
+ gemspec_stubs_in(dir, pattern) { |path| yield path, base_dir, gems_dir }
72
+ end
73
+ end
74
+
75
+ # Also add these as class methods
76
+ extend self
77
+ end
@@ -0,0 +1,3 @@
1
+ module Carbonado
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carbonado
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Owen Mooney
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-11-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Activate installed gems at runtime. This means that gems can optionally
14
+ use other installed gems at runtime.
15
+ email:
16
+ - omooney@tcd.ie
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - ".rspec"
23
+ - ".travis.yml"
24
+ - CODE_OF_CONDUCT.md
25
+ - Gemfile
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - bin/console
30
+ - carbonado.gemspec
31
+ - lib/carbonado.rb
32
+ - lib/carbonado/version.rb
33
+ homepage: https://github.com/slicedpan/carbonado.git
34
+ licenses:
35
+ - MIT
36
+ metadata:
37
+ homepage_uri: https://github.com/slicedpan/carbonado.git
38
+ source_code_uri: https://github.com/slicedpan/carbonado
39
+ changelog_uri: https://github.com/slicedpan/carbonado/CHANGELOG.md
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubygems_version: 3.0.2
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Activate installed gems at runtime
59
+ test_files: []