enumerize 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +18 -0
- data/.travis.yml +6 -0
- data/Gemfile +9 -0
- data/MIT-LICENSE +22 -0
- data/README.md +49 -0
- data/Rakefile +11 -0
- data/enumerize.gemspec +19 -0
- data/lib/enumerize.rb +18 -0
- data/lib/enumerize/attribute.rb +32 -0
- data/lib/enumerize/integrations.rb +6 -0
- data/lib/enumerize/integrations/active_record.rb +27 -0
- data/lib/enumerize/integrations/basic.rb +55 -0
- data/lib/enumerize/value.rb +21 -0
- data/lib/enumerize/version.rb +3 -0
- data/test/active_record/migrate/1_create_tables.rb +8 -0
- data/test/active_record/models.rb +7 -0
- data/test/active_record_test.rb +38 -0
- data/test/basic_test.rb +75 -0
- data/test/test_helper.rb +6 -0
- data/test/value_test.rb +18 -0
- metadata +87 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Sergey Nartimov
|
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,49 @@
|
|
1
|
+
# Enumerize
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'enumerize'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install enumerize
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Basic:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
class User
|
25
|
+
include Enumerize
|
26
|
+
|
27
|
+
enumerize :sex, :in => [:male, :female]
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
ActiveRecord:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class User < ActiveRecord::Base
|
35
|
+
include Enumerize
|
36
|
+
|
37
|
+
enumerize :sex, :in => [:male, :female]
|
38
|
+
|
39
|
+
enumerize :role, :in => [:user, :admin], :default => :user
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
1. Fork it
|
46
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
47
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
48
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
49
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/enumerize.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/enumerize/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Sergey Nartimov"]
|
6
|
+
gem.email = "info@twinslash.com"
|
7
|
+
gem.description = %q{Enumerated attributes with I18n and ActiveRecord support}
|
8
|
+
gem.summary = %q{Enumerated attributes with I18n and ActiveRecord support}
|
9
|
+
gem.homepage = "https://github.com/twinslash/enumerize"
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "enumerize"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Enumerize::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency('activesupport', '>= 3.1.3')
|
19
|
+
end
|
data/lib/enumerize.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'enumerize/version'
|
3
|
+
|
4
|
+
module Enumerize
|
5
|
+
autoload :Attribute, 'enumerize/attribute'
|
6
|
+
autoload :Value, 'enumerize/value'
|
7
|
+
autoload :Integrations, 'enumerize/integrations'
|
8
|
+
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
included do
|
12
|
+
if defined?(ActiveRecord::Base) && self < ActiveRecord::Base
|
13
|
+
include Integrations::ActiveRecord
|
14
|
+
else
|
15
|
+
include Integrations::Basic
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Enumerize
|
2
|
+
class Attribute
|
3
|
+
attr_reader :name, :values, :default_value
|
4
|
+
|
5
|
+
def initialize(klass, name, options={})
|
6
|
+
raise ArgumentError, ':in option is required' unless options[:in]
|
7
|
+
|
8
|
+
@klass = klass
|
9
|
+
@name = name
|
10
|
+
@values = Array(options[:in]).map { |v| Value.new(self, v) }
|
11
|
+
|
12
|
+
if options[:default]
|
13
|
+
@default_value = options[:default] && find_value(options[:default])
|
14
|
+
raise ArgumentError, 'invalid default value' unless @default_value
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def find_value(value)
|
19
|
+
return if value.nil?
|
20
|
+
value = value.to_s
|
21
|
+
values.find { |v| v == value }
|
22
|
+
end
|
23
|
+
|
24
|
+
def i18n_suffix
|
25
|
+
@klass.model_name.i18n_key if @klass.respond_to?(:model_name)
|
26
|
+
end
|
27
|
+
|
28
|
+
def options
|
29
|
+
values.map { |v| [v.text, v.to_s] }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module Enumerize
|
4
|
+
module Integrations
|
5
|
+
module ActiveRecord
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
include Integrations::Basic
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
private
|
12
|
+
|
13
|
+
def _define_enumerize_attribute(mod, attr)
|
14
|
+
mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
15
|
+
def #{attr.name}
|
16
|
+
self.class.#{attr.name}.find_value(super)
|
17
|
+
end
|
18
|
+
|
19
|
+
def #{attr.name}=(new_value)
|
20
|
+
super self.class.#{attr.name}.find_value(new_value).to_s
|
21
|
+
end
|
22
|
+
RUBY
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module Enumerize
|
4
|
+
module Integrations
|
5
|
+
module Basic
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def enumerize(*args, &block)
|
10
|
+
attr = Attribute.new(self, *args)
|
11
|
+
singleton_class.class_eval do
|
12
|
+
define_method(attr.name) { attr }
|
13
|
+
end
|
14
|
+
|
15
|
+
mod = Module.new
|
16
|
+
|
17
|
+
mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
18
|
+
def initialize(*, &_)
|
19
|
+
super
|
20
|
+
self.#{attr.name} = self.class.#{attr.name}.default_value if #{attr.name}.nil?
|
21
|
+
end
|
22
|
+
RUBY
|
23
|
+
|
24
|
+
_define_enumerize_attribute(mod, attr)
|
25
|
+
|
26
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
27
|
+
def #{attr.name}_text
|
28
|
+
#{attr.name} && #{attr.name}.text
|
29
|
+
end
|
30
|
+
RUBY
|
31
|
+
|
32
|
+
include mod
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def _define_enumerize_attribute(mod, attr)
|
38
|
+
mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
39
|
+
def #{attr.name}
|
40
|
+
if defined?(@#{attr.name})
|
41
|
+
self.class.#{attr.name}.find_value(@#{attr.name})
|
42
|
+
else
|
43
|
+
@#{attr.name} = nil
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def #{attr.name}=(new_value)
|
48
|
+
@#{attr.name} = self.class.#{attr.name}.find_value(new_value).to_s
|
49
|
+
end
|
50
|
+
RUBY
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'i18n'
|
2
|
+
|
3
|
+
module Enumerize
|
4
|
+
class Value < String
|
5
|
+
def initialize(attr, value)
|
6
|
+
singleton_class.class_eval do
|
7
|
+
define_method(:attr) { attr }
|
8
|
+
end
|
9
|
+
|
10
|
+
super(value.to_s)
|
11
|
+
freeze
|
12
|
+
end
|
13
|
+
|
14
|
+
def text
|
15
|
+
i18n_keys = ['']
|
16
|
+
i18n_keys.unshift "#{attr.i18n_suffix}." if attr.i18n_suffix
|
17
|
+
i18n_keys.map! { |k| :"enumerize.#{k}#{attr.name}.#{self}" }
|
18
|
+
I18n.t(i18n_keys.shift, :default => i18n_keys)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'active_record'
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
ActiveRecord::Migration.verbose = false
|
6
|
+
ActiveRecord::Base.logger = Logger.new(nil)
|
7
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
8
|
+
ActiveRecord::Migrator.migrate File.expand_path("../active_record/migrate", __FILE__)
|
9
|
+
require File.expand_path('../active_record/models', __FILE__)
|
10
|
+
|
11
|
+
describe Enumerize::Integrations::ActiveRecord do
|
12
|
+
it 'sets nil if invalid value is passed' do
|
13
|
+
user = User.new
|
14
|
+
user.sex = :invalid
|
15
|
+
user.sex.must_equal nil
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'saves value' do
|
19
|
+
User.delete_all
|
20
|
+
user = User.new
|
21
|
+
user.sex = :female
|
22
|
+
user.save!
|
23
|
+
user.sex.must_equal 'female'
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'loads value' do
|
27
|
+
User.delete_all
|
28
|
+
I18n.backend.store_translations(:en, :enumerize => {:sex => {:male => 'Male'}})
|
29
|
+
User.create!(:sex => :male)
|
30
|
+
user = User.first
|
31
|
+
user.sex.must_equal 'male'
|
32
|
+
user.sex_text.must_equal 'Male'
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'has default value' do
|
36
|
+
User.new.role.must_equal 'user'
|
37
|
+
end
|
38
|
+
end
|
data/test/basic_test.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe Enumerize::Integrations::Basic do
|
4
|
+
let(:klass) do
|
5
|
+
Class.new do
|
6
|
+
include Enumerize
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
let(:object) { klass.new }
|
11
|
+
|
12
|
+
it 'defines method that returns nil' do
|
13
|
+
klass.enumerize(:foo, :in => [:a, :b])
|
14
|
+
object.foo.must_equal nil
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'defines setter method' do
|
18
|
+
klass.enumerize(:foo, :in => [:a, :b])
|
19
|
+
object.foo = :a
|
20
|
+
object.foo.must_equal 'a'
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'returns translation' do
|
24
|
+
I18n.backend.store_translations(:en, :enumerize => {:foo => {:a => 'a text'}})
|
25
|
+
klass.enumerize(:foo, :in => [:a, :b])
|
26
|
+
object.foo = :a
|
27
|
+
object.foo.text.must_equal 'a text'
|
28
|
+
object.foo_text.must_equal 'a text'
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'returns nil as translation when value is nil' do
|
32
|
+
I18n.backend.store_translations(:en, :enumerize => {:foo => {:a => 'a text'}})
|
33
|
+
klass.enumerize(:foo, :in => [:a, :b])
|
34
|
+
object.foo_text.must_equal nil
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'scopes translation by i18 key' do
|
38
|
+
I18n.backend.store_translations(:en, :enumerize => {:example_class => {:foo => {:a => 'a text scoped'}}})
|
39
|
+
def klass.model_name
|
40
|
+
name = "ExampleClass"
|
41
|
+
def name.i18n_key
|
42
|
+
'example_class'
|
43
|
+
end
|
44
|
+
|
45
|
+
name
|
46
|
+
end
|
47
|
+
klass.enumerize(:foo, :in => [:a, :b])
|
48
|
+
object.foo = :a
|
49
|
+
object.foo.text.must_equal 'a text scoped'
|
50
|
+
object.foo_text.must_equal 'a text scoped'
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'returns options for select' do
|
54
|
+
I18n.backend.store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}})
|
55
|
+
klass.enumerize(:foo, :in => [:a, :b])
|
56
|
+
klass.foo.options.must_equal [['a text', 'a'], ['b text', 'b']]
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'stores value as string' do
|
60
|
+
klass.enumerize(:foo, :in => [:a, :b])
|
61
|
+
object.foo = :a
|
62
|
+
object.instance_variable_get(:@foo).must_be_instance_of String
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'handles default value' do
|
66
|
+
klass.enumerize(:foo, :in => [:a, :b], :default => :b)
|
67
|
+
object.foo.must_equal 'b'
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'raises exception on invalid default value' do
|
71
|
+
proc {
|
72
|
+
klass.enumerize(:foo, :in => [:a, :b], :default => :c)
|
73
|
+
}.must_raise ArgumentError
|
74
|
+
end
|
75
|
+
end
|
data/test/test_helper.rb
ADDED
data/test/value_test.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
describe Enumerize::Value do
|
5
|
+
let(:value) { Enumerize::Value.new(nil, 'test_value') }
|
6
|
+
|
7
|
+
it 'is a string' do
|
8
|
+
value.must_be_kind_of String
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'is compared to string' do
|
12
|
+
value.must_be :==, 'test_value'
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'is frozen' do
|
16
|
+
value.must_be :frozen?
|
17
|
+
end
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enumerize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Sergey Nartimov
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: &21707780 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.1.3
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *21707780
|
25
|
+
description: Enumerated attributes with I18n and ActiveRecord support
|
26
|
+
email: info@twinslash.com
|
27
|
+
executables: []
|
28
|
+
extensions: []
|
29
|
+
extra_rdoc_files: []
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- .travis.yml
|
33
|
+
- Gemfile
|
34
|
+
- MIT-LICENSE
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- enumerize.gemspec
|
38
|
+
- lib/enumerize.rb
|
39
|
+
- lib/enumerize/attribute.rb
|
40
|
+
- lib/enumerize/integrations.rb
|
41
|
+
- lib/enumerize/integrations/active_record.rb
|
42
|
+
- lib/enumerize/integrations/basic.rb
|
43
|
+
- lib/enumerize/value.rb
|
44
|
+
- lib/enumerize/version.rb
|
45
|
+
- test/active_record/migrate/1_create_tables.rb
|
46
|
+
- test/active_record/models.rb
|
47
|
+
- test/active_record_test.rb
|
48
|
+
- test/basic_test.rb
|
49
|
+
- test/test_helper.rb
|
50
|
+
- test/value_test.rb
|
51
|
+
homepage: https://github.com/twinslash/enumerize
|
52
|
+
licenses: []
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
hash: -2173772140920833240
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
hash: -2173772140920833240
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 1.8.15
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: Enumerated attributes with I18n and ActiveRecord support
|
81
|
+
test_files:
|
82
|
+
- test/active_record/migrate/1_create_tables.rb
|
83
|
+
- test/active_record/models.rb
|
84
|
+
- test/active_record_test.rb
|
85
|
+
- test/basic_test.rb
|
86
|
+
- test/test_helper.rb
|
87
|
+
- test/value_test.rb
|