enum_accessor 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +131 -0
- data/Rakefile +6 -0
- data/enum_accessor.gemspec +25 -0
- data/lib/enum_accessor/railtie.rb +11 -0
- data/lib/enum_accessor/version.rb +3 -0
- data/lib/enum_accessor.rb +88 -0
- data/spec/enum_accessor_spec.rb +64 -0
- data/spec/locales.yml +6 -0
- data/spec/spec_helper.rb +15 -0
- metadata +125 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Kenn Ejima
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
# EnumAccessor - Simple enum fields for ActiveRecord
|
2
|
+
|
3
|
+
EnumAccessor lets you define enum for attributes, and store them as integer in the database.
|
4
|
+
|
5
|
+
Compatible with ActiveRecord 3 or later.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile.
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'enum_accessor'
|
13
|
+
```
|
14
|
+
|
15
|
+
Add an integer column.
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
create_table :users do |t|
|
19
|
+
t.column :gender, :integer, default: 0
|
20
|
+
end
|
21
|
+
```
|
22
|
+
|
23
|
+
Define `enum_accessor` in a model class.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class User < ActiveRecord::Base
|
27
|
+
enum_accessor :gender, [ :female, :male ]
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
And now you have a set of methods and constants.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
user = User.new
|
35
|
+
user.gender # => :female
|
36
|
+
user.gender_male? # => false
|
37
|
+
user.gender_raw # => 0
|
38
|
+
|
39
|
+
user.gender = :male
|
40
|
+
user.gender_male? # => true
|
41
|
+
user.gender_raw # => 1
|
42
|
+
|
43
|
+
User.genders # => { :female => 0, :male => 1 }
|
44
|
+
User::GENDERS # => { "female" => 0, "male" => 1 }
|
45
|
+
```
|
46
|
+
|
47
|
+
Notice that zero-based numbering is used for database values.
|
48
|
+
|
49
|
+
## Manual coding
|
50
|
+
|
51
|
+
There are times when it makes more sense to manually pick particular integers for the mapping.
|
52
|
+
|
53
|
+
Just pass a hash with coded integer values.
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
enum_accessor :status, ok: 200, not_found: 404, internal_server_error: 500
|
57
|
+
```
|
58
|
+
|
59
|
+
## Scoping query
|
60
|
+
|
61
|
+
To retrieve internal integer values for query, use `User.genders`.
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
User.where(gender: User.genders(:female))
|
65
|
+
```
|
66
|
+
|
67
|
+
## Validations
|
68
|
+
|
69
|
+
You can pass custom validation options to `validates_inclusion_of`.
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
enum_accessor :status, [ :on, :off ], validation_options: { message: "incorrect status" }
|
73
|
+
```
|
74
|
+
|
75
|
+
Or skip validation entirely.
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
enum_accessor :status, [ :on, :off ], validate: false
|
79
|
+
```
|
80
|
+
|
81
|
+
## i18n
|
82
|
+
|
83
|
+
EnumAccessor supports i18n just as ActiveModel does.
|
84
|
+
|
85
|
+
Add the following lines to `config/locales/ja.yml`
|
86
|
+
|
87
|
+
```yaml
|
88
|
+
ja:
|
89
|
+
enum_accessor:
|
90
|
+
user:
|
91
|
+
gender:
|
92
|
+
female: 女
|
93
|
+
male: 男
|
94
|
+
```
|
95
|
+
|
96
|
+
and now `human_*` method returns a translated string. It defaults to English nicely as well.
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
I18n.locale = :ja
|
100
|
+
user.human_gender # => '女'
|
101
|
+
|
102
|
+
I18n.locale = :en
|
103
|
+
user.human_gender # => 'Female'
|
104
|
+
```
|
105
|
+
|
106
|
+
## Why enum keys are internally stored as strings rather than symbols?
|
107
|
+
|
108
|
+
Because `params[:gender].to_sym` is dangerous. It could be a source of problems like memory leak, slow symbol table lookup, or even DoS attack. If a user sends random strings for the parameter, it generates unlimited number of symbols, which can never be garbage collected, and eventually causes `symbol table overflow (RuntimeError)`, eating up gigabytes of memory.
|
109
|
+
|
110
|
+
For the same reason, `ActiveSupport::HashWithIndifferentAccess` (which is used for `params`) keeps hash keys as string internally.
|
111
|
+
|
112
|
+
https://github.com/rails/rails/blob/master/activesupport/lib/active_support/hash_with_indifferent_access.rb
|
113
|
+
|
114
|
+
## Other solutions
|
115
|
+
|
116
|
+
There are tons of similar gems out there. Then why did I bother creating another one myself rather than sending pull requests to one of them? Because each one of them has incompatible design policies than EnumAccessor.
|
117
|
+
|
118
|
+
* [simple_enum](https://github.com/lwe/simple_enum)
|
119
|
+
* Pretty close to EnumAccessor feature-wise but requires `*_cd` suffix for the database column, which makes AR scopes ugly.
|
120
|
+
* [enum_field](https://github.com/jamesgolick/enum_field)
|
121
|
+
* Enum values are defined as top-level predicate methods, which could conflict with existing methods. Also you can't define multiple enums to the same model. In some use cases, predicate methods are not necessary and you just want to be on the safe side.
|
122
|
+
* [enumerated_attribute](https://github.com/jeffp/enumerated_attribute)
|
123
|
+
* Top-level predicate methods. Many additional methods are coupled with a specific usage assumption.
|
124
|
+
* [coded_options](https://github.com/jasondew/coded_options)
|
125
|
+
* No support for symbols. Verbose definitions.
|
126
|
+
* [active_enum](https://github.com/adzap/active_enum)
|
127
|
+
* Syntax seems verbose.
|
128
|
+
* [classy_enum](https://github.com/beerlington/classy_enum)
|
129
|
+
* As the name suggests, class-based enum. I wanted something lighter.
|
130
|
+
|
131
|
+
Also, EnumAccessor has one of the simplest code base, so that you can easily hack on.
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'enum_accessor/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = 'enum_accessor'
|
8
|
+
gem.version = EnumAccessor::VERSION
|
9
|
+
gem.authors = ['Kenn Ejima']
|
10
|
+
gem.email = ['kenn.ejima@gmail.com']
|
11
|
+
gem.description = %q{Enum field support for ActiveModel with validations and i18n}
|
12
|
+
gem.summary = %q{Enum field support for ActiveModel with validations and i18n}
|
13
|
+
gem.homepage = ''
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ['lib']
|
19
|
+
|
20
|
+
gem.add_runtime_dependency 'activesupport', '>= 3.0.0'
|
21
|
+
|
22
|
+
gem.add_development_dependency 'activerecord', '>= 3.0.0'
|
23
|
+
gem.add_development_dependency 'rspec'
|
24
|
+
gem.add_development_dependency 'sqlite3'
|
25
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'enum_accessor/version'
|
2
|
+
require 'enum_accessor/railtie'
|
3
|
+
require 'active_support'
|
4
|
+
|
5
|
+
module EnumAccessor
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def enum_accessor(field, enums, options={})
|
10
|
+
# Normalize arguments
|
11
|
+
field = field.to_s
|
12
|
+
case enums
|
13
|
+
when Array
|
14
|
+
enums = Hash[enums.map.with_index{|v,i| [v.to_s, i] }]
|
15
|
+
when Hash
|
16
|
+
enums = Hash[enums.map{|k,v| [k.to_s, v] }]
|
17
|
+
else
|
18
|
+
raise ArgumentError.new('enum_accessor takes Array or Hash as the second argument')
|
19
|
+
end
|
20
|
+
|
21
|
+
const_name = field.pluralize.upcase
|
22
|
+
const_set(const_name, enums) unless const_defined?(const_name)
|
23
|
+
const = const_get(const_name)
|
24
|
+
|
25
|
+
symbolized_enums = Hash[enums.map{|k,v| [k.to_sym, v] }]
|
26
|
+
|
27
|
+
# Getter
|
28
|
+
define_method(field) do
|
29
|
+
const.key(read_attribute(field)).try(:to_sym)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Setter
|
33
|
+
define_method("#{field}=") do |arg|
|
34
|
+
write_attribute field, const[arg.to_s]
|
35
|
+
end
|
36
|
+
|
37
|
+
# Raw-value getter
|
38
|
+
define_method("#{field}_raw") do
|
39
|
+
read_attribute field
|
40
|
+
end
|
41
|
+
|
42
|
+
# Raw-value setter
|
43
|
+
define_method("#{field}_raw=") do |arg|
|
44
|
+
write_attribute field, Integer(arg)
|
45
|
+
end
|
46
|
+
|
47
|
+
# Checker
|
48
|
+
symbolized_enums.keys.each do |key|
|
49
|
+
method_name = key.to_s.downcase.gsub(/[-\s]/, '_')
|
50
|
+
define_method("#{field}_#{method_name}?") do
|
51
|
+
self.send(field) == key
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
56
|
+
def self.#{field.pluralize}(*args)
|
57
|
+
return #{symbolized_enums} if args.first.nil?
|
58
|
+
return #{symbolized_enums}[args.first.to_sym] if args.size == 1
|
59
|
+
args.map{|arg| #{symbolized_enums}[arg.to_sym] }
|
60
|
+
end
|
61
|
+
EOS
|
62
|
+
|
63
|
+
# Human-friendly print
|
64
|
+
define_method("human_#{field}") do
|
65
|
+
self.class.human_enum_accessor(field, self.send(field))
|
66
|
+
end
|
67
|
+
|
68
|
+
# Validation
|
69
|
+
unless options[:validate] == false
|
70
|
+
validates_inclusion_of field, { :in => symbolized_enums.keys }.merge(options[:validation_options] || {})
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# Mimics ActiveModel::Translation.human_attribute_name
|
75
|
+
def human_enum_accessor(field, value, options = {})
|
76
|
+
defaults = lookup_ancestors.map do |klass|
|
77
|
+
:"#{self.i18n_scope}.enum_accessor.#{klass.model_name.i18n_key}.#{field}.#{value}"
|
78
|
+
end
|
79
|
+
defaults << :"enum_accessor.#{self.model_name.i18n_key}.#{field}.#{value}"
|
80
|
+
defaults << :"enum_accessor.#{field}.#{value}"
|
81
|
+
defaults << options.delete(:default) if options[:default]
|
82
|
+
defaults << value.to_s.humanize
|
83
|
+
|
84
|
+
options.reverse_merge! :count => 1, :default => defaults
|
85
|
+
I18n.translate(defaults.shift, options)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
ActiveRecord::Base.connection.create_table :users, force: true do |t|
|
6
|
+
t.column :gender, :integer, default: 0
|
7
|
+
end
|
8
|
+
|
9
|
+
class User < ActiveRecord::Base
|
10
|
+
enum_accessor :gender, [ :female, :male ]
|
11
|
+
end
|
12
|
+
|
13
|
+
describe EnumAccessor do
|
14
|
+
before do
|
15
|
+
@user = User.new
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'adds checker' do
|
19
|
+
@user.gender_female?.should == true
|
20
|
+
@user.gender_male?.should == false
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'adds getter' do
|
24
|
+
@user.gender.should == :female
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'adds setter' do
|
28
|
+
@user.gender = :male
|
29
|
+
@user.gender_male?.should == true
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'adds raw value getter' do
|
33
|
+
@user.gender_raw.should == 0
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'adds raw value setter' do
|
37
|
+
@user.gender_raw = 1
|
38
|
+
@user.gender_male?.should == true
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'adds humanized getter' do
|
42
|
+
I18n.locale = :ja
|
43
|
+
@user.human_gender.should == '女'
|
44
|
+
|
45
|
+
I18n.locale = :en
|
46
|
+
@user.human_gender.should == 'Female'
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'defines internal constant' do
|
50
|
+
User::GENDERS.should == { "female" => 0, "male" => 1 }
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'adds class methods' do
|
54
|
+
User.genders.should == { :female => 0, :male => 1 }
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'adds validation' do
|
58
|
+
@user.gender = 'bogus'
|
59
|
+
@user.valid?.should be_false
|
60
|
+
|
61
|
+
@user.gender = 'male'
|
62
|
+
@user.valid?.should be_true
|
63
|
+
end
|
64
|
+
end
|
data/spec/locales.yml
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
4
|
+
require 'enum_accessor'
|
5
|
+
|
6
|
+
RSpec.configure do |config|
|
7
|
+
require 'active_record'
|
8
|
+
ActiveRecord::Base.send(:include, EnumAccessor)
|
9
|
+
|
10
|
+
# Establish in-memory database connection
|
11
|
+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
|
12
|
+
|
13
|
+
# Load translation
|
14
|
+
I18n.load_path << File.join(File.dirname(__FILE__), 'locales.yml')
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enum_accessor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kenn Ejima
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activerecord
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 3.0.0
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 3.0.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: sqlite3
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Enum field support for ActiveModel with validations and i18n
|
79
|
+
email:
|
80
|
+
- kenn.ejima@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- .rspec
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE.txt
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- enum_accessor.gemspec
|
92
|
+
- lib/enum_accessor.rb
|
93
|
+
- lib/enum_accessor/railtie.rb
|
94
|
+
- lib/enum_accessor/version.rb
|
95
|
+
- spec/enum_accessor_spec.rb
|
96
|
+
- spec/locales.yml
|
97
|
+
- spec/spec_helper.rb
|
98
|
+
homepage: ''
|
99
|
+
licenses: []
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ! '>='
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
requirements: []
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 1.8.24
|
119
|
+
signing_key:
|
120
|
+
specification_version: 3
|
121
|
+
summary: Enum field support for ActiveModel with validations and i18n
|
122
|
+
test_files:
|
123
|
+
- spec/enum_accessor_spec.rb
|
124
|
+
- spec/locales.yml
|
125
|
+
- spec/spec_helper.rb
|