Sunsetter
A Ruby gem that helps you mark Mongoid model fields as deprecated and display warning messages when they are used.
Installation
Add this line to your application's Gemfile:
gem 'sunsetter'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install sunsetter
Usage
To mark a field as deprecated in your Mongoid model:
class User
include Mongoid::Document
include Sunsetter
field :old_name
field :new_name
deprecate_field :old_name
end
When the deprecated field is accessed, you'll see warning messages like this:
[SUNSETTER] User#old_name is deprecated and will be removed in a future version.
[SUNSETTER] Called from: app/models/user.rb:42:in `some_method'
[SUNSETTER] Please update your code to use alternative methods.
Customizing Log Output
You can customize how deprecation warnings are logged:
# In Rails, you might want to use Rails.logger
Sunsetter.configure do |config|
config.logger = ->(message) { Rails.logger.warn(message) }
end
# Or use your own logger
Sunsetter.configure do |config|
config.logger = ->(message) { MyLogger.warn(message) }
end
# Or suppress warnings entirely
Sunsetter.configure do |config|
config.logger = ->(message) { } # no-op
end
Features
- Displays warnings when deprecated fields are accessed
- Shows caller information (file name and line number)
- Prevents usage in non-Mongoid::Document models
- Implementation is independent of include order
- Customizable logging behavior
Development
Setup
$ git clone https://github.com/ryoh827/sunsetter.git
$ cd sunsetter
$ bundle install
Running Tests
$ bundle exec ruby test/test_sunsetter.rb
License
The gem is available as open source under the terms of the MIT License.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request