Skip to content

zhisme/lazy_names

Repository files navigation

rake Gem Version Gem Total Downloads Hits-of-Code codecov GitHub License

lazy_names

lazy_names 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

The idea is to reduce typing of long namespaced constants to shorter versions. It's very useful when you have a lot of nested namespaces and you need to access them frequently. This gem will take your responsibility to redefine constants to shorter versions and making constant/classes validations.

Why Plain Ruby? (v2.0+)

LazyNames v2.0 uses plain Ruby instead of YAML because:

  • Intuitive: Same syntax you'd write in .irbrc/.pryrc
  • IDE Support: Syntax highlighting and autocomplete work out of the box
  • Validation: Constants are validated at load time
  • Simpler: No YAML parsing, no extra abstraction
  • Ruby-native: Write Ruby to configure Ruby

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
touch ~/.lazy_names.rb
# or for project-specific config
touch .lazy_names.rb
  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

Create a .lazy_names.rb file in your project root or home directory:

# .lazy_names.rb
MUCC = Models::Users::CreditCard
SPP = Services::PaymentProcessor
CAVUC = Controllers::API::V1::UsersController

File Lookup Priority

LazyNames looks for configuration in this order:

  1. ./.lazy_names.rb (project-specific) — recommended
  2. ~/.lazy_names.rb (global fallback)

Project-Specific Configuration (Recommended)

This is the preferred approach. Create .lazy_names.rb in your project root:

# myproject/.lazy_names.rb
MUCC = Models::Users::CreditCard
SPP = Services::PaymentProcessor

Why project-specific files are better:

  • Version controlled with your project (or gitignored for personal shortcuts)
  • Explicit about what's loaded — no surprises
  • Each project has exactly the shortcuts it needs
  • No conflicts between projects

Don't forget to add it to .gitignore if you don't want to share:

echo '.lazy_names.rb' >> .gitignore

Global Configuration

For shortcuts that work across multiple projects, create ~/.lazy_names.rb in your home directory.

Important: Use if defined?() guards since constants vary between projects:

# ~/.lazy_names.rb
# Generic Rails shortcuts (usually available)
AR = ActiveRecord if defined?(ActiveRecord)
AM = ActionMailer if defined?(ActionMailer)

# Only defined if these exist in the current project
MU = Models::User if defined?(Models::User)

Note: v2.0 intentionally removed namespace/project scoping that existed in v1.x YAML configs. The if defined?() pattern is simpler and achieves the same result without magic.

Validation

LazyNames validates each line:

  • ✅ Must be a constant assignment: SHORT = Full::Constant::Path
  • ✅ Constant must exist in your application
  • ⚠️ Invalid lines are skipped with a warning

Examples

# Comments are allowed
MUCC = Models::Users::CreditCard

# Blank lines are fine

SPP = Services::PaymentProcessor

# Underscores and numbers in short names
API_V1 = API::V1
CACHE2 = Cache::RedisCache

Migrating from v1.x to v2.0

LazyNames v2.0 removes YAML support in favor of plain Ruby configuration.

Upgrading from v1.x? See the complete Migration Guide for:

  • Automated conversion script
  • Step-by-step instructions
  • Troubleshooting tips
  • New features in v2.0

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. After you make changes ensure to run rake rubocop to check if your code meets linter standards.

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. Ensure the CI build is green by validating tests are passing and coverage is not decreased.

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

 
 
 

Contributors