# Labelizer
[![Build Status](https://travis-ci.org/getto-systems/labelizer.svg?branch=master)](https://travis-ci.org/getto-systems/labelizer)
[![Gem Version](https://badge.fury.io/rb/labelizer.svg)](https://badge.fury.io/rb/labelizer)
add labels to attribute
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'labelizer'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install labelizer
## Usage
```ruby
# app/models/my_model.rb
class Customer < ApplicationModel
enum registration_state: {
starting: 0,
confirming: 1,
completed: 2,
}
include Labelizer
labelize :registration_state, %w(label description color)
end
```
```yaml
# config/locales/labelizer.ja.yml
ja:
labelizer:
customer:
registration_state:
starting:
label: start
description: registration starting...
color: label label-info
confirming:
label: start
description: registration confirming...
color: label label-warning
completed:
label: start
description: registration completed!!
color: label label-success
```
```erb
# app/views/customers/show.html.erb
<% customer = Customer.find id %>
<%# status label %>
<%= customer.registration_state_label %>
<% labels = customer.registration_state_labelized %>
<%= labels.label %>
<% labels = customer.labelized.registration_state %>
<%= labels.label %>
<% labels = customer.labelized[:registration_state] %>
<%= labels[:label] %>
<%# description %>
<% Customer.registration_state_labelized.each do |state,labels| %>
- <%= labels.label %> : <%= labels.description %>
<% end %>
<% Customer.labelized.registration_state.each do |state,labels| %>
- <%= labels.label %> : <%= labels.description %>
<% end %>
<% Customer.labelized[:registration_state].each do |state,labels| %>
- <%= labels[:label] %> : <%= labels[:description] %>
<% end %>
```
```ruby
Customer.labelized[:unknown_attr] #=> KeyError
Customer.labelized[:registration_state]["unknown_state"] #=> KeyError
Customer.labelized[:registration_state]["starting"][:unknown_label_type] #=> KeyError
```
## Converters
Convert label:
```ruby
labelize :registration_state, %w(label description color), converter: {
color: ->(value){
# value : attribute value
"label label-#{value}"
},
}
```
```yaml
ja:
labelizer:
customer:
registration_state:
starting:
color: info
```
```ruby
Customer.starting.last.color # => "label label-info"
```
## default labels
```yaml
ja:
labelizer:
color: ... # <= global default
customer:
color: ... # <= model default
registration_state:
color: ... # <= attribute default
starting:
color: ...
```
## without enum
```ruby
class Customer < ApplicationModel
include Labelizer
def self.registration_states
{
"starting" => 0,
"confirming" => 1,
"completed" => 2,
}
end
labelize :registration_state, %w(label description color)
end
```
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` 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](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/getto-systems/labelizer.
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).