Sha256: 868d42800a1c7f729c511d6fa13b0b0660d5061a728f31dd176e5c6d4d647b70

Contents?: true

Size: 1.19 KB

Versions: 18

Compression:

Stored size: 1.19 KB

Contents

require 'fukuzatsu'

class Breathalizer

  INDICATORS = [
    :if,
    :def,
  ]

  attr_accessor :path_to_file, :edges, :nodes, :exits, :accumulator

  def self.parse!(path_to_file)
    new(path_to_file).parse
  end

  def initialize(path_to_file)
    self.accumulator = []
    self.edges = 0
    self.nodes = 1
    self.exits = 1
    self.path_to_file = path_to_file
  end

  def file_contents
    contents ||= File.open(path_to_file, "r").read
  end

  def parsed
    @parsed ||= Parser::CurrentRuby.parse(file_contents)
  end

  def parse!
    traverse(parsed)
    complexity
  end

  def traverse(node)#, accumulator=[])

    accumulator << node.type

    if node.type == :if || node.type == :begin
      self.edges += 2
      self.nodes += 2
      self.exits += 1
    elsif node.type == :def
      self.edges += 1
      self.nodes += 1
      self.exits += 1
    end

    node.children.each do |child|
      if child.respond_to?(:type) || child.respond_to?(:children)
        accumulator << child.type
        traverse(child, accumulator)
      else
      end
    end
  end

  def complexity
    p "edges = #{self.edges}, nodes = #{self.nodes}, exits = #{self.exits}"
    self.edges - self.nodes + exits
  end

end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
fukuzatsu-1.0.6 spec/fixtures/program_3.rb
fukuzatsu-1.0.5 spec/fixtures/program_3.rb
fukuzatsu-1.0.4 spec/fixtures/program_3.rb
fukuzatsu-1.0.3 spec/fixtures/program_3.rb
fukuzatsu-1.0.2 spec/fixtures/program_3.rb
fukuzatsu-1.0.1 spec/fixtures/program_3.rb
fukuzatsu-1.0.0 spec/fixtures/program_3.rb
fukuzatsu-0.10.1 spec/fixtures/program_3.rb
fukuzatsu-0.9.16 spec/fixtures/program_3.rb
fukuzatsu-0.9.15 spec/fixtures/program_3.rb
fukuzatsu-0.9.14 spec/fixtures/program_3.rb
fukuzatsu-0.9.13 spec/fixtures/program_3.rb
fukuzatsu-0.9.12 spec/fixtures/program_3.rb
fukuzatsu-0.9.11 spec/fixtures/program_3.rb
fukuzatsu-0.9.10 spec/fixtures/program_3.rb
fukuzatsu-0.9.8 spec/fixtures/program_3.rb
fukuzatsu-0.9.7 spec/fixtures/program_3.rb
fukuzatsu-0.9.6 spec/fixtures/program_3.rb