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



112
113
114
# File 'lib/gem_bench/player.rb', line 112

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

#check_line(file_path, line_match) ⇒ Object



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

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



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

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



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

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

#inspectObject



79
80
81
# File 'lib/gem_bench/player.rb', line 79

def inspect
  to_s(:name)
end

#semverObject



83
84
85
86
87
# File 'lib/gem_bench/player.rb', line 83

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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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 = !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)


56
57
58
# File 'lib/gem_bench/player.rb', line 56

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

#suggest(num) ⇒ Object



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

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

#to_s(format = :name) ⇒ Object



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

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