0.0
No commit activity in last 3 years
No release in over 3 years
DebugMethods is a module that lets you define environment specific methods in your Ruby on Rails app so you can build yourself useful inspection utilities in development without adding weight to your classes in production.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.16
~> 10.0
~> 3.0
 Project Readme

DebugMethods

DebugMethods is a module that lets you define environment specific methods in your Ruby on Rails app so you can build yourself useful inspection utilities in development without adding weight to your classes in production.

Installation

Add this line to your Rails application's Gemfile:

gem 'debug_methods'

And then execute:

$ bundle

Usage

Just include DebugMethods in any class and wrap your debug methods in a block being passed to #debug_methods.

class Thing < ApplicationRecord
  include DebugMethods

  debug_methods do
    # expensive method that puts out attributes in pretty format
    def list_attrs
      longest_key_length = attributes.keys.max { |a, b| a.to_s.length <=> b.to_s.length }.length
      puts
      attributes.sort.to_h.each do |k, v|
        spaces_offset = longest_key_length - k.to_s.length
        spaces = ' ' * spaces_offset
        puts "#{k}#{spaces}\t#{v == nil ? 'nil' : v}"
      end
    end
  end

end

Then your debug_methods (like #list_attrs above) will only be allowed in the development environment.

$ RAILS_ENV="development" rails console
>> person = Person.new(name: "Jake", age: "30")
>> person.list_attrs

age             30
created_at	nil
id        	nil
name      	Jake
updated_at	nil

$ RAILS_ENV="test" rails console
>> person = Person.new(name: "Jake", age: "30")
>> person.list_attrs
NoMethodError (undefined method `list_attrs' for #<Thing id: nil, name: "Jake",
age: 30, created_at: nil, updated_at: nil>)

DebugMethods defaults to only including methods in the :development environment. To configure which environments debug methods should be available in, add an initializer. Add the file config/initializers/debug_methods.rb and include the following:

DebugMethods.configure do |config|
  config.environments << :test
end

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 tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/debug_methods.

License

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