Class: GemBench::StrictVersionRequirement

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

Overview

Helps enforce a version requirement for every dependency in a Gemfile

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ StrictVersionRequirement



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/gem_bench/strict_version_requirement.rb', line 6

def initialize(options = {})
  @gemfile_path = options.fetch(:gemfile_path, "#{Dir.pwd}/Gemfile")
  file = File.open(gemfile_path)
  # Get all lines as an array
  all_lines = file.readlines
  @gems = []
  all_lines.each_with_index do |line, index|
    # will return nil if the line is not a gem line
    gem = StrictVersionGem.from_line(all_lines, line, index)
    @gems << gem if gem
  end

  @starters, @benchers = @gems.partition { |x| x.valid? }
  # Remove all the commented || blank lines
  @verbose = options[:verbose]
  self.print if verbose
end

Instance Attribute Details

#benchersObject (readonly)

Returns the value of attribute benchers.



4
5
6
# File 'lib/gem_bench/strict_version_requirement.rb', line 4

def benchers
  @benchers
end

#gemfile_pathObject (readonly)

Returns the value of attribute gemfile_path.



4
5
6
# File 'lib/gem_bench/strict_version_requirement.rb', line 4

def gemfile_path
  @gemfile_path
end

#gemsObject (readonly)

Returns the value of attribute gems.



4
5
6
# File 'lib/gem_bench/strict_version_requirement.rb', line 4

def gems
  @gems
end

#startersObject (readonly)

Returns the value of attribute starters.



4
5
6
# File 'lib/gem_bench/strict_version_requirement.rb', line 4

def starters
  @starters
end

#verboseObject (readonly)

Returns the value of attribute verbose.



4
5
6
# File 'lib/gem_bench/strict_version_requirement.rb', line 4

def verbose
  @verbose
end

Instance Method Details

#find(name) ⇒ Object



32
33
34
# File 'lib/gem_bench/strict_version_requirement.rb', line 32

def find(name)
  gems.detect { |x| x.name == name }
end

#gem_at(index) ⇒ Object



36
37
38
# File 'lib/gem_bench/strict_version_requirement.rb', line 36

def gem_at(index)
  gems.detect { |x| x.index == index }
end

#list_missing_version_constraintsObject



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

def list_missing_version_constraints
  benchers.map { |x| x.name }
end


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/gem_bench/strict_version_requirement.rb', line 40

def print
  using_path = benchers.count { |x| x.is_type?(:path) }
  puts "    \#{\"      \"}\n    The gems that need to be improved are:\n\n    \#{benchers.map(&:to_s).join(\"\\n\")}\n\n    There are \#{starters.length} gems that have valid strict version constraints.\n    Of those:\n      \#{starters.count { |x| x.is_type?(:constraint) }} use primary constraints (e.g. '~> 1.2.3').\n      \#{starters.count { |x| x.is_type?(:git_ref) }} use git ref constraints.\n      \#{starters.count { |x| x.is_type?(:git_tag) }} use git tag constraints.\n\n    There are \#{benchers.length} gems that do not have strict version constraints.\n    Of those:\n      \#{benchers.count { |x| x.is_type?(:git_branch) }} use git branch constraints.\n      \#{benchers.count { |x| x.is_type?(:git) }} use some other form of git constraint considered not strict enough.\n      \#{benchers.count { |x| x.is_type?(:unknown) }} gems seem to not have any constraint at all.\n      \#{using_path} gems are using a local path. \#{\"WARNING!!!\" if using_path > 0}\n  EOS\nend\n"

#versions_present?Boolean



24
25
26
# File 'lib/gem_bench/strict_version_requirement.rb', line 24

def versions_present?
  gems.detect { |x| !x.valid? }.nil?
end