Class: GemBench::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/gem_bench/player.rb

Constant Summary collapse

SEMVER_SPLIT_ON_POINT_LENGTH =

MAJOR.MINOR split on point length == 2 MAJOR.MINOR.PATCH split on point length == 3 Semver 2.0 Standard is to accept minor and patch updates

2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Player

Returns a new instance of Player.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/gem_bench/player.rb', line 10

def initialize(options = {})
  @name = options[:name]
  @version = options[:version]
  @exclude_file_pattern = options[:exclude_file_pattern]
  @state = nil
  @stats = []
  @file_path_glob = GemBench::PATH_GLOB.call(@name)
  # Used to find the line of the Gemfile which creates the primary dependency on this gem
  @gemfile_regex = GemBench::DEPENDENCY_REGEX_PROC.call(@name)
  @checked = false
end

Instance Attribute Details

#checkedObject (readonly)

Returns the value of attribute checked.



8
9
10
# File 'lib/gem_bench/player.rb', line 8

def checked
  @checked
end

#exclude_file_patternObject (readonly)

Returns the value of attribute exclude_file_pattern.



8
9
10
# File 'lib/gem_bench/player.rb', line 8

def exclude_file_pattern
  @exclude_file_pattern
end

#file_path_globObject (readonly)

Returns the value of attribute file_path_glob.



8
9
10
# File 'lib/gem_bench/player.rb', line 8

def file_path_glob
  @file_path_glob
end

#gemfile_regexObject (readonly)

Returns the value of attribute gemfile_regex.



8
9
10
# File 'lib/gem_bench/player.rb', line 8

def gemfile_regex
  @gemfile_regex
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#stateObject

Returns the value of attribute state.



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

def state
  @state
end

#statsObject

Returns the value of attribute stats.



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

def stats
  @stats
end

#versionObject

Returns the value of attribute version.



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

def version
  @version
end

Instance Method Details

#careful(num) ⇒ Object



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

def careful(num)
  "\t[BE CAREFUL] #{num}) #{how}"
end

#howObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gem_bench/player.rb', line 82

def how
  case self.state
  when GemBench::PLAYER_STATES[:starter] then
    to_s(:semver)
  when GemBench::PLAYER_STATES[:bench] then
    "#{to_s(:semver)}, require: false"
  else
    if checked
      "#{self} is feeling very lost right now."
    else
      "#{self} had no files to evaluate."
    end
  end
end

#info(num) ⇒ Object



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

def info(num)
  "\t[INFO] #{num}) #{how}"
end

#inspectObject



70
71
72
# File 'lib/gem_bench/player.rb', line 70

def inspect
  to_s(:name)
end

#semverObject



74
75
76
77
78
79
80
# File 'lib/gem_bench/player.rb', line 74

def semver
  ver = version
  until ver.split(".").length <= SEMVER_SPLIT_ON_POINT_LENGTH do
    ver = ver[0..(ver.rindex(".")-1)]
  end
  ver
end

#set_starter(file_path, line_match: nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/gem_bench/player.rb', line 22

def set_starter(file_path, line_match: nil)
  return false if file_path =~ exclude_file_pattern
  # Some gems may have zero files to check, as they may be using gem as a
  #   delivery system for shell scripts!  As such we need to check which
  #   gems got checked, and which had nothing to check
  @checked = true
  line_match ||= GemBench::RAILTIE_REGEX
  scan = begin
    if GemBench::DO_NOT_SCAN.include?(name)
      false
    else
      begin
        File.read(file_path).encode('utf-8', :invalid => :replace, :undef => :replace, :replace => '_') =~ line_match
      rescue ArgumentError => e
        if e.message =~ /invalid byte sequence/
          puts "[GemBench] checking #{file_path} failed due to unparseable file content"
          false # Assume the likelihood of files with encoding issues that also contain railtie to be low, so: false.
        end
      end
    end
  end
  self.stats << [file_path,scan] if scan
  self.state = !!scan ?
    GemBench::PLAYER_STATES[:starter] :
    GemBench::PLAYER_STATES[:bench]
end

#starter?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/gem_bench/player.rb', line 49

def starter?
  self.state == GemBench::PLAYER_STATES[:starter]
end

#suggest(num) ⇒ Object



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

def suggest(num)
  "\t[SUGGESTION] #{num}) #{how}"
end

#to_s(format = :name) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/gem_bench/player.rb', line 53

def to_s(format = :name)
  case format
    when :name then
      name
    when :v then
      "#{name} v#{version}"
    when :semver
      "gem '#{name}', '~> #{semver}'"
    when :locked
      "gem '#{name}', '#{version}'"
    when :legacy # when depending on legacy gems, you specifically want to not upgrade, except patches.
      "gem '#{name}', '~> #{version}'"
    when :upgrade # when upgrading, and testing gem compatibility you want to try anything newer
      "gem '#{name}', '>= #{version}'"
  end
end