Commit 8d901aa8 authored by Coraline Ehmke's avatar Coraline Ehmke

First step in integrating old

parent 80383ac2
......@@ -12,11 +12,13 @@ require_relative "snuffle/formatters/csv"
require_relative "snuffle/formatters/html"
require_relative "snuffle/formatters/html_index"
require_relative "snuffle/formatters/text"
require_relative "snuffle/latent_object"
require_relative "snuffle/line_of_code"
require_relative "snuffle/node"
require_relative "snuffle/source_file"
require_relative "snuffle/summary"
require_relative "snuffle/elements/hash"
require_relative "snuffle/elements/method_definition"
require_relative "snuffle/util/histogram"
module Snuffle
......
class Snuffle::Element::MethodDefinition
attr_accessor :node
def self.materialize(nodes=[])
nodes.each.map{|node| new(node) }
end
def initialize(node)
self.node = node
end
def method_name
node.name
end
end
module Snuffle
class LatentObject
# def suspicious_methods
# @suspicious_methods ||=
# methods_owned_by_klass.select{ |m| (words_in_method(m.to_s) & potential_objects).size > 0 }
# end
DUPLICATE_THRESHOLD = 1
def self.methods_from(nodes)
Snuffle::Element::MethodDefinition.materialize(nodes.methods)
end
def self.suspicious_methods(nodes, threshold=DUPLICATE_THRESHOLD)
# results = Snuffle::Util::Histogram::from(arrays)
# nodes.methods.map(&:name)
# select{ |m| (words_in_method(m.to_s) & potential_objects).size > 0 }
names = methods_from(nodes).map(&:method_name)
words = names.map.inject({}) do |words, method_name|
name.split('_').each do |word|
words[word] ||= []
words[word] << method_name
end
words
end
method_words = words.keys
dupes = Snuffle::Util::Histogram::from(method_words)
dupes.select{|k,v| v > threshold }
end
def self.potential_objects
@potential_objects ||=
begin
common_words = find_common_words_in(hash_of_words_used_in_methods)
words_used_more_than_twice(common_words)
end
end
private
def words_used_more_than_twice(hash_of_words = {})
hash_of_words.select{ |k| k.size > 2 }
end
def hash_of_words_used_in_methods
@hash_of_words_used_in_methods ||=
methods_owned_by_klass.inject({}) do |hash, method|
hash[method] = words_in_method(method)
hash
end
end
def find_common_words_in(hash_of_words = hash_of_words_used_in_methods)
count_word_frequency(hash_of_words).select{ |k,v| v > 1 }.keys
end
def count_word_frequency(hash_of_words = {})
hash_of_words.values.flatten.inject({}) do |hash, word|
hash[word] ||= 0
hash[word] += 1
hash
end
end
def words_in_method(name)
name.to_s.gsub(/[^a-z\_]/i,'').split('_')
end
end
end
\ No newline at end of file
......@@ -11,6 +11,7 @@ module Snuffle
scope :by_type, lambda{|type| where(:type => type)}
scope :with_parent, lambda{|parent_id| where(parent_id: parent_id) }
scope :hashes, {type: :hash}
scope :methods, {is_method: true}
def self.nil
new(type: :nil)
......@@ -33,6 +34,10 @@ module Snuffle
Snuffle::Node.where(parent_id: self.id)
end
def is_method
self.type == :def || self.type == :defs
end
def inspect
{
id: self.id,
......
......@@ -75,7 +75,7 @@ module Snuffle
extracted_node = Snuffle::Node.new(
type: ast_node.type,
parent_id: parent_id,
name: name_from(ast_node),
name: name,
line_numbers: lines.map(&:line_number)
)
else
......@@ -98,9 +98,7 @@ module Snuffle
if name_coords = node.loc.name
name = source[name_coords.begin_pos, name_coords.end_pos - 1]
return unless name =~ /[a-zA-Z]/
return name
else
"?"
return name.gsub(/^([A-Za-z0-9\_]+)(.+)$/m, '\1')
end
else
return name_from(node.children.last)
......
class Account
attr_accessor :user
def initialize(user)
self.user = user
end
def status
end
def active?
end
def user_name
end
def user_address
end
def user_email
end
end
\ No newline at end of file
......@@ -6,6 +6,9 @@ class Customer
MY_CONSTANT = "TheOtherZachIsThePrimaryZach"
def self.api_root
end
def my_condition
puts "MAGIC" if true == false
end
......
require 'spec_helper'
describe Snuffle::LatentObject do
let(:program_2) { Snuffle::SourceFile.new(path_to_file: "spec/fixtures/latent_object_fixture.rb") }
describe ".suspicious_methods" do
it "detects namespaced methods"
end
end
\ No newline at end of file
......@@ -6,6 +6,7 @@ describe Snuffle::SourceFile do
let(:program_2) { Snuffle::SourceFile.new(path_to_file: "spec/fixtures/program_2.rb") }
let(:program_3) { Snuffle::SourceFile.new(path_to_file: "spec/fixtures/program_3.rb") }
let(:program_4) { Snuffle::SourceFile.new(path_to_file: "spec/fixtures/program_4.rb") }
describe "#cohorts" do
it "does not match hash values with non-hash values" do
......@@ -27,6 +28,17 @@ describe Snuffle::SourceFile do
end
describe "#name_from" do
let(:program) { Snuffle::SourceFile.new(path_to_file: "spec/fixtures/latent_object_fixture.rb") }
let(:node) { program.send(:ast).children[2].children[1] }
it "pulls the name of a method" do
expect(program.send(:name_from, node)).to eq('initialize')
end
end
describe "#class_name" do
let(:top_level) { "require 'something'; class Foo; def bar; puts 'hi'; end; end"}
......
require 'snuffle'
def source_file
@source_file ||= Snuffle::SourceFile.new(path_to_file: "spec/fixtures/program_2.rb")
@source_file ||= Snuffle::SourceFile.new(path_to_file: "spec/fixtures/latent_object_fixture.rb")
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment