Class: RubyVersionReader

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

Constant Summary collapse

VERSION =
'0.1.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(given_path, given_environment_manager = 'rvm') ⇒ RubyVersionReader

Returns a new instance of RubyVersionReader.



9
10
11
12
13
14
# File 'lib/ruby_version_reader.rb', line 9

def initialize(given_path, given_environment_manager = 'rvm')
  given_path = './' if given_path.empty?
  @path = File.expand_path(given_path)

  @environment_manager = given_environment_manager
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

#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



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

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



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

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

#environment_manager_load_stringObject



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

def environment_manager_load_string
  case environment_manager
  when 'rvm'
    "rvm use #{to_s}"
  when 'rbenv'
    "RBENV_VERSION=#{to_s}"
  when 'chruby'
    "chruby-exec #{to_s} --"
  else
    raise "Unsupported environment_manager: #{environment_manager.inspect}"
  end
end

#full_versionObject



99
100
101
# File 'lib/ruby_version_reader.rb', line 99

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

#gemsetObject



26
27
28
# File 'lib/ruby_version_reader.rb', line 26

def gemset
  read_ruby_gemset_file
end

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

chaining for dsl-like language

Returns:

  • (Boolean)


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

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

#majorObject Also known as: main



103
104
105
# File 'lib/ruby_version_reader.rb', line 103

def major
  full_version.to_i
end

#minorObject Also known as: mini



108
109
110
# File 'lib/ruby_version_reader.rb', line 108

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

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



88
89
90
# File 'lib/ruby_version_reader.rb', line 88

def not(other)
  self != other
end

#patchlevelObject



118
119
120
# File 'lib/ruby_version_reader.rb', line 118

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

#tinyObject Also known as: teeny



113
114
115
# File 'lib/ruby_version_reader.rb', line 113

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

#to_sObject Also known as: inspect



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

def to_s
  return @to_s if @to_s

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

  @to_s
end