Class: AbcSize::Calculator

Inherits:
Object
  • Object
show all
Defined in:
lib/abc_size/calculator.rb

Overview

main class

Constant Summary collapse

SATISFACTORY_ABC_SIZE =
17

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_code: nil, path: nil, parameters: nil) ⇒ Calculator

Returns a new instance of Calculator.



14
15
16
17
18
19
20
21
22
# File 'lib/abc_size/calculator.rb', line 14

def initialize(source_code: nil, path: nil, parameters: nil)
  @source_code = source_code
  @path = path
  @parameters = parameters

  @discount = @parameters.map { |parameter| ['-d', '--discount'].include?(parameter) }.any?(true)

  @results = []
end

Instance Attribute Details

#discountObject (readonly)

Returns the value of attribute discount.



12
13
14
# File 'lib/abc_size/calculator.rb', line 12

def discount
  @discount
end

#parametersObject (readonly)

Returns the value of attribute parameters.



12
13
14
# File 'lib/abc_size/calculator.rb', line 12

def parameters
  @parameters
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/abc_size/calculator.rb', line 12

def path
  @path
end

#resultsObject (readonly)

Returns the value of attribute results.



12
13
14
# File 'lib/abc_size/calculator.rb', line 12

def results
  @results
end

#ruby_versionObject (readonly)

Returns the value of attribute ruby_version.



12
13
14
# File 'lib/abc_size/calculator.rb', line 12

def ruby_version
  @ruby_version
end

#source_codeObject (readonly)

Returns the value of attribute source_code.



12
13
14
# File 'lib/abc_size/calculator.rb', line 12

def source_code
  @source_code
end

Instance Method Details

#callObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/abc_size/calculator.rb', line 24

def call
  source = source_code || read_source_code_from_file
  @ruby_version = return_ruby_version

  nodes = RuboCop::AST::ProcessedSource.new(source, ruby_version).ast
  nodes.each_node { |node| results << calculate_result(node) if node.is_a?(RuboCop::AST::DefNode) }

  print_all_messages

  # return results for testing purposes
  results
end