Skip to content

Ease your console typing with redefining constants by shorter version

License

Notifications You must be signed in to change notification settings

zhisme/lazy_names

Repository files navigation

lazy_names

LazyNames helps programmer to type faster very-long class names, constants by defining them on a shorter version. If you are lazy like me typing many times a day in console long constants then this gem will ease your development process.

Why

Consider this example from pry terminal session. Lazy names in action

Installation

  1. Add this line to your application's Gemfile:
group :development do
  gem 'lazy_names'
end
  1. Setup your console to automatically load lazy names
  • If you are using pry, add this line to ~/.pryrc or per project myproject/.pryrc
if defined?(LazyNames)
  Pry.config.hooks.add_hook(:when_started, :lazy_names) do
    LazyNames.load_definitions!
  end
end
  • If you are using irb, add this line to ~/.irbrc or per project myproject/.irbrc
if defined?(LazyNames)
  LazyNames.load_definitions!
end
  1. And then execute:
bundle
  1. Create your own lazy_names config where you define constants
cp .lazy_names.tt.yml ~/.lazy_names.yml
  1. Login into your rails or non-rails console
$ bundle exec rails c # or bin/console
# your shorter version of constants are available now, enjoy!

Configuration

Global definition

Take a look onto lazy_names.tt.yml it has very basic template for you to start.

---
my_awesome_project:
  definitions:
    'Models::Users::CreditCard': 'MUCC'

my_awesome_project should be you project/folder name

So consider this example:

$ pwd
/Users/name/my_awesome_project

The last folder name of you ruby project must match the same one in your configuration. After definitions sections you can actually redefine your long constants. So in this example Models::Users::CreditCard is your real project constant and MUCC will be your short variant of it, so you can access Models::Users::CreditCard from MUCC. MUCC and any other right hand side can be any value, you define the best-suitable names.

You can define as many constants as you want. The same rule applies for projects. Your config can have multiple constant definitions per namespace.

---
my_awesome_project:
  definitions:
    'Models::Users::CreditCard': 'MUCC'
my_another_project:
  definitions:
    'OtherLongConst': 'Short'

Project definitions

In the meantime you can put your .lazy_names.yml config directly to project folder, it will be looked up firstly from project. Just do not forget to put in your .gitignore. I believe every developer defines shorter versions of constants by his own opinion.

echo '.lazy_names.yml' >> .gitignore

If project folder doesn't contain any .lazy_names.yml, it will fallback to home directory.

Configuration per project a bit different: you don't need to specify global scope my_awesome_project, you can skip forward to definitions

---
definitions:
  'Models::Users::CreditCard: 'MUCC'

Example config can be found in .lazy_names.tt.project.yml

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.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/zhisme/lazy_names.

License

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

About

Ease your console typing with redefining constants by shorter version

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published