Class: Resquire::Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/resquire/analyzer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Analyzer

When we create a new Analyzer, we can specifiy the gems we want to work with, but it must be passed in as an array in the args hash.

Typical Usage

analyzer = Resquire::Analyzer.new(:gems => ['packetfu', 'pcaprub'])


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/resquire/analyzer.rb', line 15

def initialize(args = {})
  @gems = []
  @redundant_gems = []
  if args[:gems]
    if args[:gems].is_a? Array
      add_gems(args[:gems])
    else
      raise Resquire::ResquireError.new('Dependencies must be initialized as an array!')
    end
  end
  true
end

Instance Attribute Details

#gemsObject (readonly)

gem dependencies, as an array



5
6
7
# File 'lib/resquire/analyzer.rb', line 5

def gems
  @gems
end

Instance Method Details

#add_gem(gem) ⇒ Object

We can add one gem at a time as a string.

Typical Usage

analyzer = Resquire::Analyzer.new
analyzer.add_gem('packetfu')
analyzer.add_gem('pcaprub')


48
49
50
51
52
# File 'lib/resquire/analyzer.rb', line 48

def add_gem(gem)
  @gems << gem
  @gems = @gems.uniq
  true
end

#add_gems(gems = []) ⇒ Object

We can add gems specified from an array.

Typical Usage

analyzer = Resquire::Analyzer.new
analyzer.add_gems(['packetfu', 'pcaprub'])


35
36
37
38
# File 'lib/resquire/analyzer.rb', line 35

def add_gems(gems = [])
  gems.uniq.each { |gem| @gems << gem } 
  true
end

#optimized_gemsObject

Once we have figured out the redundant gems, we can easily check out our optimized gem list which we can now use.

Typical Usage

analyzer = Resquire::Analyzer.new(:gems => ['packetfu', 'pcaprub'])
analyzer.redundant_gems
# => ['pcaprub']
analyzer.optimized_gems
# => ['packetfu']


99
100
101
102
# File 'lib/resquire/analyzer.rb', line 99

def optimized_gems
  optimized_gems = @gems.reject { |gem| @redundant_gems.include? gem }
  optimized_gems.empty? ? false : optimized_gems.uniq.sort
end

#permutations(gems = @gems) ⇒ Object

We can check the given permutations that we will need to cycle through to find gems that are redundant.

Typical Usage

analyzer = Resquire::Analyzer.new(:gems => ['packetfu', 'pcaprub'])
analyzer.permutations


63
64
65
# File 'lib/resquire/analyzer.rb', line 63

def permutations(gems = @gems)
  (1..gems.count).reduce(:*) || 1
end

#redundant_gems(args = {}) ⇒ Object

We can find the redundant gems from the the analyzer has to work with, iterating over the possible permutations and returning an array of redundant gems.

Typical Usage

analyzer = Resquire::Analyzer.new(:gems => ['packetfu', 'pcaprub'])
analyzer.permutations
# => 2


77
78
79
80
81
82
83
84
85
# File 'lib/resquire/analyzer.rb', line 77

def redundant_gems(args = {})
  find_redundant_gems_with_gem(args)
  @count = 0
  @permutation_count = permutations
  @progress_bar = args[:progress_bar] || true
  gems = args[:gems] || @gems
  redundant = args[:redundant_gems] || @redundant_gems
  find_redundant_gems(args).empty? ? false : @redundant_gems
end