Class: GemBench::Player

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

Overview

Each gem either needs to be required at boot time or not. Player helps determine which gems can use ‘require: false` in the Gemfile to cut down load times.

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.



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

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.



10
11
12
# File 'lib/gem_bench/player.rb', line 10

def checked
  @checked
end

#exclude_file_patternObject (readonly)

Returns the value of attribute exclude_file_pattern.



10
11
12
# File 'lib/gem_bench/player.rb', line 10

def exclude_file_pattern
  @exclude_file_pattern
end

#file_path_globObject (readonly)

Returns the value of attribute file_path_glob.



10
11
12
# File 'lib/gem_bench/player.rb', line 10

def file_path_glob
  @file_path_glob
end

#gemfile_regexObject (readonly)

Returns the value of attribute gemfile_regex.



10
11
12
# File 'lib/gem_bench/player.rb', line 10

def gemfile_regex
  @gemfile_regex
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#stateObject

Returns the value of attribute state.



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

def state
  @state
end

#statsObject

Returns the value of attribute stats.



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

def stats
  @stats
end

#versionObject

Returns the value of attribute version.



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

def version
  @version
end

Instance Method Details

#careful(num) ⇒ Object



114
115
116
# File 'lib/gem_bench/player.rb', line 114

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

#check_line(file_path, line_match) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gem_bench/player.rb', line 41

def check_line(file_path, line_match)
  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.
  else
    puts "[GemBench] checking #{file_path} failed. Please report a bug to https://github.com/pboling/gembench/issues"
    raise e
  end
end

#howObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/gem_bench/player.rb', line 91

def how
  case state
  when GemBench::PLAYER_STATES[:starter]
    to_s(:semver)
  when GemBench::PLAYER_STATES[:bench]
    "#{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



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

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

#inspectObject



81
82
83
# File 'lib/gem_bench/player.rb', line 81

def inspect
  to_s(:name)
end

#semverObject



85
86
87
88
89
# File 'lib/gem_bench/player.rb', line 85

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

#set_starter(file_path, line_match: nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gem_bench/player.rb', line 24

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 = !GemBench::DO_NOT_SCAN.include?(name) && check_line(file_path, line_match)
  stats << [file_path, scan] if scan
  self.state = if !!scan
    GemBench::PLAYER_STATES[:starter]
  else
    GemBench::PLAYER_STATES[:bench]
  end
end

#starter?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/gem_bench/player.rb', line 58

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

#suggest(num) ⇒ Object



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

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

#to_s(format = :name) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/gem_bench/player.rb', line 62

def to_s(format = :name)
  case format
  when :name
    name
  when :v
    "#{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}'"
  else
    raise ArgumentError, "Unknown format for #{self.class.name}#to_s"
  end
end