Renamespace

CI status Gem Version

A command-line tool to help Ruby developers refactor class/module namespacing.

Contents

Intro

Renamespaces a Ruby source file:

  • Moves the file
  • Updates, to match the new location, the name of the class/module within the file, including its namespacing
  • Updates usages of the class/module
  • Updates the path to the file in all requires
  • Moves the associated spec file

Class/module namespaces are derived from the paths provided.

If you change the number of namespaces, expect to have to run RuboCop autocorrect afterwards to clean up formatting.

The namespace replacing is not super smart yet, so it might get it wrong sometimes. If it does, see renamespace --help for some options that might help.

Installation

The executable is distributed as a gem. You can install it from RubyGems directly like so:

$ gem install renamespace

And then if you're using rbenv:

$ rbenv rehash

Usage

$ renamespace SOURCE_FILE_PATH DESTINATION_FILE_PATH

Example

With:

# lib/my_app/models/site.rb

module MyApp
  module Models
    class Site < BaseModel
    end
  end
end

Run:

$ renamespace lib/my_app/models/site.rb lib/my_app/sites/model.rb

Result:

# lib/my_app/sites/model.rb

module MyApp
  module Sites
    class Model < Models::BaseModel
    end
  end
end

More usage info

See:

$ renamespace --help

Development

Pre-push hook

This hook runs style checks and tests.

To set up the pre-push hook:

$ echo -e "#\!/bin/bash\n\$(dirname \$0)/../../auto/pre-push-hook" > .git/hooks/pre-push
chmod +x .git/hooks/pre-push

Release

To release a new version:

$ auto/release/update-version && auto/release/tag && auto/release/publish

This takes care of the whole process:

  • Incrementing the version number (the patch number by default)
  • Tagging & pushing commits
  • Publishing the gem to RubyGems
  • Creating a draft GitHub release

To increment the minor or major versions instead of the patch number, run auto/release/update-version with --minor or --major.