Class: Bundler::RubyVersion

Inherits:
Object
  • Object
show all
Defined in:
lib/bundler/ruby_version.rb

Constant Summary

PATTERN =
/
  ruby\s
  ([\d.]+) # ruby version
  (?:p(-?\d+))? # optional patchlevel
  (?:\s\((\S+)\s(.+)\))? # optional engine info
/xo

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(versions, patchlevel, engine, engine_version) ⇒ RubyVersion

Returns a new instance of RubyVersion



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bundler/ruby_version.rb', line 11

def initialize(versions, patchlevel, engine, engine_version)
  # The parameters to this method must satisfy the
  # following constraints, which are verified in
  # the DSL:
  #
  # * If an engine is specified, an engine version
  #   must also be specified
  # * If an engine version is specified, an engine
  #   must also be specified
  # * If the engine is "ruby", the engine version
  #   must not be specified, or the engine version
  #   specified must match the version.

  @versions           = Array(versions)
  @gem_version        = Gem::Requirement.create(@versions.first).requirements.first.last
  @input_engine       = engine
  @engine             = engine || "ruby"
  @engine_versions    = (engine_version && Array(engine_version)) || @versions
  @engine_gem_version = Gem::Requirement.create(@engine_versions.first).requirements.first.last
  @patchlevel         = patchlevel
end

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine



4
5
6
# File 'lib/bundler/ruby_version.rb', line 4

def engine
  @engine
end

#engine_gem_versionObject (readonly)

Returns the value of attribute engine_gem_version



4
5
6
# File 'lib/bundler/ruby_version.rb', line 4

def engine_gem_version
  @engine_gem_version
end

#engine_versionsObject (readonly)

Returns the value of attribute engine_versions



4
5
6
# File 'lib/bundler/ruby_version.rb', line 4

def engine_versions
  @engine_versions
end

#gem_versionObject (readonly)

Returns the value of attribute gem_version



4
5
6
# File 'lib/bundler/ruby_version.rb', line 4

def gem_version
  @gem_version
end

#patchlevelObject (readonly)

Returns the value of attribute patchlevel



4
5
6
# File 'lib/bundler/ruby_version.rb', line 4

def patchlevel
  @patchlevel
end

#versionsObject (readonly)

Returns the value of attribute versions



4
5
6
# File 'lib/bundler/ruby_version.rb', line 4

def versions
  @versions
end

Class Method Details

.from_string(string) ⇒ RubyVersion, Nil

Returns a RubyVersion from the given string.

Parameters:

  • the (String)

    version string to match.

Returns:

  • (RubyVersion, Nil)

    The version if the string is a valid RubyVersion description, and nil otherwise.



53
54
55
# File 'lib/bundler/ruby_version.rb', line 53

def self.from_string(string)
  new($1, $2, $3, $4) if string =~ PATTERN
end

.systemObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/bundler/ruby_version.rb', line 99

def self.system
  ruby_engine = if defined?(RUBY_ENGINE) && !RUBY_ENGINE.nil?
    RUBY_ENGINE.dup
  else
    # not defined in ruby 1.8.7
    "ruby"
  end
  # :sob: mocking RUBY_VERSION breaks stuff on 1.8.7
  ruby_version = ENV.fetch("BUNDLER_SPEC_RUBY_VERSION") { RUBY_VERSION }.dup
  ruby_engine_version = case ruby_engine
                        when "ruby"
                          ruby_version
                        when "rbx"
                          Rubinius::VERSION.dup
                        when "jruby"
                          JRUBY_VERSION.dup
                        else
                          raise BundlerError, "RUBY_ENGINE value #{RUBY_ENGINE} is not recognized"
  end
  patchlevel = RUBY_PATCHLEVEL.to_s

  @ruby_version ||= RubyVersion.new(ruby_version, patchlevel, ruby_engine, ruby_engine_version)
end

Instance Method Details

#==(other) ⇒ Object



61
62
63
64
65
66
# File 'lib/bundler/ruby_version.rb', line 61

def ==(other)
  versions == other.versions &&
    engine == other.engine &&
    engine_versions == other.engine_versions &&
    patchlevel == other.patchlevel
end

#diff(other) ⇒ Object

Returns a tuple of these things:

[diff, this, other]
The priority of attributes are
1. engine
2. ruby_version
3. engine_version

Raises:

  • (ArgumentError)


82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/bundler/ruby_version.rb', line 82

def diff(other)
  raise ArgumentError, "Can only diff with a RubyVersion, not a #{other.class}" unless other.is_a?(RubyVersion)
  if engine != other.engine && @input_engine
    [:engine, engine, other.engine]
  elsif versions.empty? || !matches?(versions, other.gem_version)
    [:version, versions_string(versions), versions_string(other.versions)]
  elsif @input_engine && !matches?(engine_versions, other.engine_gem_version)
    [:engine_version, versions_string(engine_versions), versions_string(other.engine_versions)]
  elsif patchlevel && (!patchlevel.is_a?(String) || !other.patchlevel.is_a?(String) || !matches?(patchlevel, other.patchlevel))
    [:patchlevel, patchlevel, other.patchlevel]
  end
end

#hostObject



68
69
70
71
72
73
74
# File 'lib/bundler/ruby_version.rb', line 68

def host
  @host ||= [
    RbConfig::CONFIG["host_cpu"],
    RbConfig::CONFIG["host_vendor"],
    RbConfig::CONFIG["host_os"]
  ].join("-")
end

#single_version_stringObject



57
58
59
# File 'lib/bundler/ruby_version.rb', line 57

def single_version_string
  to_s(gem_version)
end

#to_gem_version_with_patchlevelObject



123
124
125
126
127
128
129
# File 'lib/bundler/ruby_version.rb', line 123

def to_gem_version_with_patchlevel
  @gem_version_with_patch ||= begin
    Gem::Version.create("#{@gem_version}.#{@patchlevel}")
  rescue ArgumentError
    @gem_version
  end
end

#to_s(versions = self.versions) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/bundler/ruby_version.rb', line 33

def to_s(versions = self.versions)
  output = String.new("ruby #{versions_string(versions)}")
  output << "p#{patchlevel}" if patchlevel
  output << " (#{engine} #{versions_string(engine_versions)})" unless engine == "ruby"

  output
end

#versions_string(versions) ⇒ Object



95
96
97
# File 'lib/bundler/ruby_version.rb', line 95

def versions_string(versions)
  Array(versions).join(", ")
end