CalledUncalled

Identify which methods for a class are being used and which are not.

Supports

  • Directly defined methods
  • Inherited methods
  • Singleton methods
  • Methods included from Modules
  • Methods dynamically added

Installation

Install the gem and add to the application's Gemfile by executing:

bundle add called_uncalled

If bundler is not being used to manage dependencies, install the gem by executing:

gem install called_uncalled

Usage

Include called_uncalled in your Class at any location. Execute your code as normal. When you are interested in what methods have been called or not called use the class methods 'called_methods' and 'uncalled_methods'. The class methods accept an array of classes or modules in the class hierarchy to filter by. The default filter is the class itself. To list the singleton methods, pass in the results of #singleton_class. Additionally you can pass in an instance of your class to get both the class and singleton methods combined.

Please note that

Example

module Baz

  def included_called
    puts 'in included_called'
  end

  def included_uncalled
    puts 'in included_uncalled'
  end

end

class Bar

  def inherited_called
    puts 'in inherited_called'
  end

  def inherited_uncalled
    puts 'in inherited_uncalled'
  end

end

class Foo < Bar

  include Baz

  def before_called
    puts 'in before_called'
  end

  def before_uncalled
    puts 'in before_uncalled'
  end

  include CalledUncalled

  def after_called
    puts 'in after_called'
  end

  def after_uncalled
    puts 'in after_uncalled'
  end

end

a = Foo.new
b = Foo.new
def a.singleton_called
  puts 'in singleton_called'
end

def b.singleton_uncalled
  puts 'in singleton_uncalled'
end

a.before_called
a.after_called
a.singleton_called
a.included_called
a.inherited_called

uts "\nInstance methods on #{a.class} that were called: #{Foo.called_methods}"
puts "Instance methods on #{a.class} that were not called: #{Foo.uncalled_methods}\n\n"

puts "Singleton methods on 'a' that were called: #{Foo.called_methods(a.singleton_class)}"
puts "Combined instance and singleton methods called: #{Foo.called_methods(a)}\n\n"

puts "Uncalled singleton methods on 'a': #{Foo.uncalled_methods(a.singleton_class)}"
puts "Uncalled singleton methods on 'b': #{Foo.uncalled_methods(b.singleton_class)}\n\n"

puts "Methods from Foo and Bar that were called: #{Foo.called_methods(Foo, Bar)}\n\n"
puts "All methods called #{Foo.called_methods(:all)}\n\n"

Gives the following output

in before_called
in after_called
in singleton_called
in included_called
in inherited_called

Instance methods on Foo that were called: [:before_called, :after_called]
Instance methods on Foo that were not called: [:before_uncalled, :after_uncalled]

Singleton methods on 'a' that were called: [:singleton_called]
Combined instance and singleton methods called: [:before_called, :after_called, :singleton_called]

Uncalled singleton methods on 'a': []
Uncalled singleton methods on 'b': [:singleton_uncalled]

Methods from Foo and Bar that were called: [:before_called, :after_called, :inherited_called]

All methods called [:singleton_method, :singleton_class, :before_called, :after_called, :singleton_called, :included_called, :inherited_called, :class, :respond_to?, :is_a?]

Limitations

Identically names singleton methods on multiple instances of the same class are not currently tracked separately.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/called_uncalled. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the CalledUncalled project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.