Module: Gemika::Env
Overview
Version switches to write code that works with different versions of Ruby and gem dependencies.
Constant Summary collapse
- VERSION_PATTERN =
/(?:\d+\.)*\d+/
Instance Method Summary collapse
-
#gem?(*args) ⇒ Boolean
Check if the given gem was activated by the current gemfile.
-
#gemfile ⇒ Object
Returns the path to the gemfile for the current Ruby process.
-
#ruby ⇒ Object
Returns the current version of Ruby.
-
#ruby?(requirement) ⇒ Boolean
Check if the current version of Ruby satisfies the given requirements.
-
#travis? ⇒ Boolean
Returns whether this process is running within a TravisCI build.
-
#with_gemfile(path, *args, &block) ⇒ Object
Changes the gemfile to the given
path
, runs the givenblock
, then resets the gemfile to its original path.
Instance Method Details
#gem?(*args) ⇒ Boolean
Check if the given gem was activated by the current gemfile.
It might or might not have been require
d yet.
56 57 58 59 60 61 62 63 64 |
# File 'lib/gemika/env.rb', line 56 def gem?(*args) = args.last.is_a?(Hash) ? args.pop : {} name, requirement_string = args if [:gemfile] && !process_gemfile?([:gemfile]) gem_in_gemfile?([:gemfile], name, requirement_string) else gem_activated?(name, requirement_string) end end |
#gemfile ⇒ Object
Returns the path to the gemfile for the current Ruby process.
16 17 18 19 20 21 22 |
# File 'lib/gemika/env.rb', line 16 def gemfile if @gemfile_changed @process_gemfile else ENV['BUNDLE_GEMFILE'] end end |
#ruby ⇒ Object
Returns the current version of Ruby.
69 70 71 |
# File 'lib/gemika/env.rb', line 69 def ruby RUBY_VERSION end |
#ruby?(requirement) ⇒ Boolean
Check if the current version of Ruby satisfies the given requirements.
79 80 81 |
# File 'lib/gemika/env.rb', line 79 def ruby?(requirement) requirement_satisfied?(requirement, ruby) end |
#travis? ⇒ Boolean
Returns whether this process is running within a TravisCI build.
86 87 88 |
# File 'lib/gemika/env.rb', line 86 def travis? !!ENV['TRAVIS'] end |
#with_gemfile(path, *args, &block) ⇒ Object
Changes the gemfile to the given path
, runs the given block
, then resets
the gemfile to its original path.
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/gemika/env.rb', line 33 def with_gemfile(path, *args, &block) # Make sure that if block calls #gemfile we still return the gemfile for this # process, regardless of what's in ENV temporarily @gemfile_changed = true @process_gemfile = ENV['BUNDLE_GEMFILE'] Bundler.with_clean_env do ENV['BUNDLE_GEMFILE'] = path block.call(*args) end ensure @gemfile_changed = false ENV['BUNDLE_GEMFILE'] = @process_gemfile end |