Class: RubyVersionReader

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/ruby_version_reader.rb

Constant Summary collapse

VERSION =
'0.2.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(given_path = './', given_environment_manager = 'rvm', given_options = {}) ⇒ RubyVersionReader

Available options: *:rvm_path: The path to the rvm executable (used if given_environment_manager == ‘rvm’). Default: nil.



11
12
13
14
15
16
# File 'lib/ruby_version_reader.rb', line 11

def initialize(given_path = './', given_environment_manager = 'rvm', given_options = {})
  @path = File.expand_path(given_path)
  @environment_manager = given_environment_manager
  @options = {}
  @options[:rvm_path] = given_options[:rvm_path].to_s
end

Instance Attribute Details

#environment_managerObject

Returns the value of attribute environment_manager.



7
8
9
# File 'lib/ruby_version_reader.rb', line 7

def environment_manager
  @environment_manager
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/ruby_version_reader.rb', line 7

def options
  @options
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/ruby_version_reader.rb', line 7

def path
  @path
end

Instance Method Details

#<=>(other) ⇒ Object

comparable



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ruby_version_reader.rb', line 46

def <=>(other)
  value = case other
    when Integer
      major.to_i
    when Float
      full_version.to_f
    when String
      other_engine = extract_engine_from_string(other)

      if engine == other_engine
        other = extract_full_version_from_string(other)
        full_version
      else
        return engine <=> other_engine
      end
    else
      # A tiny bit of recursion
      return self <=> other.to_s
    end
  value <=> other
end

#engineObject

accessors



97
98
99
# File 'lib/ruby_version_reader.rb', line 97

def engine
  @engine ||= extract_engine_from_string(read_ruby_version_file)
end

#environment_manager_load_stringObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ruby_version_reader.rb', line 32

def environment_manager_load_string
  case environment_manager
  when 'rvm'
    "#{options[:rvm_path].empty? ? 'rvm' : options[:rvm_path]} #{to_s} do"
  when 'rbenv'
    "RBENV_VERSION=#{to_s}"
  when 'chruby'
    "chruby-exec #{to_s} --"
  else
    raise "Unsupported environment_manager: #{environment_manager.inspect}"
  end
end

#full_versionObject



101
102
103
# File 'lib/ruby_version_reader.rb', line 101

def full_version
  @full_version ||= extract_full_version_from_string(read_ruby_version_file)
end

#gemsetObject



28
29
30
# File 'lib/ruby_version_reader.rb', line 28

def gemset
  read_ruby_gemset_file
end

#is?(other = nil) ⇒ Boolean Also known as: is

chaining for dsl-like language

Returns:

  • (Boolean)


70
71
72
73
74
75
76
# File 'lib/ruby_version_reader.rb', line 70

def is?(other = nil)
  if other
    self == other
  else
    self
  end
end

#majorObject Also known as: main



105
106
107
# File 'lib/ruby_version_reader.rb', line 105

def major
  full_version.to_i
end

#minorObject Also known as: mini



110
111
112
# File 'lib/ruby_version_reader.rb', line 110

def minor
  full_version.split('.')[1].to_i
end

#not(other) ⇒ Object Also known as: not?



90
91
92
# File 'lib/ruby_version_reader.rb', line 90

def not(other)
  self != other
end

#patchlevelObject



120
121
122
# File 'lib/ruby_version_reader.rb', line 120

def patchlevel
  full_version.split('-')[1].to_s
end

#tinyObject Also known as: teeny



115
116
117
# File 'lib/ruby_version_reader.rb', line 115

def tiny
  full_version.split('.')[2].to_i
end

#to_sObject Also known as: inspect



18
19
20
21
22
23
24
25
# File 'lib/ruby_version_reader.rb', line 18

def to_s
  return @to_s if @to_s

  @to_s = "#{engine}-#{full_version}"
  @to_s += "@#{gemset}" unless gemset.empty?

  @to_s
end