enumerize 0.9.0 → 0.10.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +17 -0
- data/README.md +2 -0
- data/lib/enumerize.rb +7 -0
- data/lib/enumerize/activerecord.rb +0 -24
- data/lib/enumerize/attribute.rb +11 -0
- data/lib/enumerize/base.rb +12 -2
- data/lib/enumerize/scope/activerecord.rb +37 -0
- data/lib/enumerize/scope/mongoid.rb +40 -0
- data/lib/enumerize/value.rb +5 -1
- data/lib/enumerize/version.rb +1 -1
- data/test/activerecord_test.rb +6 -0
- data/test/attribute_test.rb +12 -0
- data/test/mongoid_test.rb +26 -2
- data/test/multiple_test.rb +6 -0
- data/test/value_test.rb +18 -3
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 964f689869d7570459451bae1068a737f69a7db2
|
4
|
+
data.tar.gz: b1c59894d98bd2656d01c5bca52f4dc15a7fa42a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61ca5ff5fcaac6dd6a38622fbf2af35f74896bf57f9f317cbc2b7b918814ab9b4aa5e9a6437de14b709a72b61d2d09c04c7d86a6504335faed7fd008c21b2051
|
7
|
+
data.tar.gz: 8a5e9bb95f72b0abbdd5619ace2299ed8d3001d520d747751d00a84d474ecdfb82a3fce065c6af3d9e422fe1d38cdf288e286d578a95fda836b0f02606ee4c21
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,20 @@
|
|
1
|
+
## master
|
2
|
+
|
3
|
+
### enhancements
|
4
|
+
|
5
|
+
### bug fix
|
6
|
+
|
7
|
+
## 0.10.0 (February 17, 2015) ##
|
8
|
+
|
9
|
+
### enhancements
|
10
|
+
|
11
|
+
* Add scopes support to mongoid documents (by [@nashby](https://github.com/nashby))
|
12
|
+
* Use underscore.humanize in #text to make use of Inflector acronyms (by [@mintuhouse](https://github.com/mintuhouse))
|
13
|
+
* Raise an exception when :scope option is used together with :multiple option (by [@maurogeorge](https://github.com/maurogeorge))
|
14
|
+
* Use alias_method_chain instead of overriding Class#inherited (by [@yuroyoro](https://github.com/yuroyoro))
|
15
|
+
* Shortcut methods to retrieve enumerize values (by [@CyborgMaster](https://github.com/CyborgMaster))
|
16
|
+
* Extend equality operator to support comparing with symbols and custom values (e.g. integers) (by [@CyborgMaster](https://github.com/CyborgMaster))
|
17
|
+
|
1
18
|
## 0.9.0 (December 11, 2014) ##
|
2
19
|
|
3
20
|
### enhancements
|
data/README.md
CHANGED
@@ -251,6 +251,8 @@ User.having_status(:blocked).with_sex(:male, :female)
|
|
251
251
|
# SELECT "users".* FROM "users" WHERE "users"."status" IN (2) AND "users"."sex" IN ('male', 'female')
|
252
252
|
```
|
253
253
|
|
254
|
+
:warning: It is not possible to define a scope when using the `:multiple` option. :warning:
|
255
|
+
|
254
256
|
Array-like attributes with plain ruby objects:
|
255
257
|
|
256
258
|
```ruby
|
data/lib/enumerize.rb
CHANGED
@@ -14,6 +14,11 @@ module Enumerize
|
|
14
14
|
|
15
15
|
autoload :ActiveRecordSupport, 'enumerize/activerecord'
|
16
16
|
|
17
|
+
module Scope
|
18
|
+
autoload :ActiveRecord, 'enumerize/scope/activerecord'
|
19
|
+
autoload :Mongoid, 'enumerize/scope/mongoid'
|
20
|
+
end
|
21
|
+
|
17
22
|
def self.included(base)
|
18
23
|
ActiveSupport::Deprecation.warn '`include Enumerize` was deprecated. Please use `extend Enumerize`.', caller
|
19
24
|
extended(base)
|
@@ -23,6 +28,8 @@ module Enumerize
|
|
23
28
|
base.send :include, Enumerize::Base
|
24
29
|
base.extend Enumerize::Predicates
|
25
30
|
base.extend Enumerize::ActiveRecordSupport
|
31
|
+
base.extend Enumerize::Scope::ActiveRecord if defined?(::ActiveRecord::Base)
|
32
|
+
base.extend Enumerize::Scope::Mongoid if defined?(::Mongoid::Document)
|
26
33
|
|
27
34
|
if defined?(::RailsAdmin)
|
28
35
|
require 'enumerize/integrations/rails_admin'
|
@@ -5,10 +5,6 @@ module Enumerize
|
|
5
5
|
|
6
6
|
_enumerize_module.dependent_eval do
|
7
7
|
if defined?(::ActiveRecord::Base) && self < ::ActiveRecord::Base
|
8
|
-
if options[:scope]
|
9
|
-
_define_scope_methods!(name, options)
|
10
|
-
end
|
11
|
-
|
12
8
|
include InstanceMethods
|
13
9
|
|
14
10
|
# Since Rails use `allocate` method on models and initializes them with `init_with` method.
|
@@ -21,26 +17,6 @@ module Enumerize
|
|
21
17
|
end
|
22
18
|
end
|
23
19
|
|
24
|
-
private
|
25
|
-
|
26
|
-
def _define_scope_methods!(name, options)
|
27
|
-
scope_name = options[:scope] == true ? "with_#{name}" : options[:scope]
|
28
|
-
|
29
|
-
define_singleton_method scope_name do |*values|
|
30
|
-
values = values.map { |value| enumerized_attributes[name].find_value(value).value }
|
31
|
-
values = values.first if values.size == 1
|
32
|
-
|
33
|
-
where(name => values)
|
34
|
-
end
|
35
|
-
|
36
|
-
if options[:scope] == true
|
37
|
-
define_singleton_method "without_#{name}" do |*values|
|
38
|
-
values = values.map { |value| enumerized_attributes[name].find_value(value).value }
|
39
|
-
where(arel_table[name].not_in(values))
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
20
|
module InstanceMethods
|
45
21
|
# https://github.com/brainspec/enumerize/issues/74
|
46
22
|
def write_attribute(attr_name, value)
|
data/lib/enumerize/attribute.rb
CHANGED
@@ -4,6 +4,7 @@ module Enumerize
|
|
4
4
|
|
5
5
|
def initialize(klass, name, options={})
|
6
6
|
raise ArgumentError, ':in option is required' unless options[:in]
|
7
|
+
raise ArgumentError, ':scope option does not work with option :multiple' if options[:multiple] && options[:scope]
|
7
8
|
|
8
9
|
extend Multiple if options[:multiple]
|
9
10
|
|
@@ -25,6 +26,12 @@ module Enumerize
|
|
25
26
|
@default_value = find_default_value(options[:default])
|
26
27
|
raise ArgumentError, 'invalid default value' unless @default_value
|
27
28
|
end
|
29
|
+
|
30
|
+
# Define methods to get each value
|
31
|
+
@values.each do |v|
|
32
|
+
metaclass = class << self; self; end
|
33
|
+
metaclass.send(:define_method, v.to_s) { v }
|
34
|
+
end
|
28
35
|
end
|
29
36
|
|
30
37
|
def find_default_value(value)
|
@@ -39,6 +46,10 @@ module Enumerize
|
|
39
46
|
@value_hash[value.to_s] unless value.nil?
|
40
47
|
end
|
41
48
|
|
49
|
+
def find_values(*values)
|
50
|
+
values.map { |value| find_value(value) }.compact
|
51
|
+
end
|
52
|
+
|
42
53
|
def i18n_scopes
|
43
54
|
@i18n_scopes ||= if i18n_scope
|
44
55
|
scopes = Array(i18n_scope)
|
data/lib/enumerize/base.rb
CHANGED
@@ -6,6 +6,14 @@ module Enumerize
|
|
6
6
|
if base.respond_to?(:validate)
|
7
7
|
base.validate :_validate_enumerized_attributes
|
8
8
|
end
|
9
|
+
|
10
|
+
class << base
|
11
|
+
if (method_defined?(:inherited) || private_method_defined?(:inherited)) && (not method_defined? :inherited_without_enumerized)
|
12
|
+
alias_method :inherited_without_enumerized, :inherited
|
13
|
+
end
|
14
|
+
|
15
|
+
alias_method :inherited, :inherited_with_enumerized
|
16
|
+
end
|
9
17
|
end
|
10
18
|
|
11
19
|
module ClassMethods
|
@@ -28,9 +36,11 @@ module Enumerize
|
|
28
36
|
@enumerized_attributes ||= AttributeMap.new
|
29
37
|
end
|
30
38
|
|
31
|
-
def
|
39
|
+
def inherited_with_enumerized(subclass)
|
32
40
|
enumerized_attributes.add_dependant subclass.enumerized_attributes
|
33
|
-
|
41
|
+
if respond_to?(:inherited_without_enumerized, true)
|
42
|
+
inherited_without_enumerized subclass
|
43
|
+
end
|
34
44
|
end
|
35
45
|
|
36
46
|
private
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Enumerize
|
2
|
+
module Scope
|
3
|
+
module ActiveRecord
|
4
|
+
def enumerize(name, options={})
|
5
|
+
super
|
6
|
+
|
7
|
+
_enumerize_module.dependent_eval do
|
8
|
+
if self < ::ActiveRecord::Base
|
9
|
+
if options[:scope]
|
10
|
+
_define_scope_methods!(name, options)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def _define_scope_methods!(name, options)
|
19
|
+
scope_name = options[:scope] == true ? "with_#{name}" : options[:scope]
|
20
|
+
|
21
|
+
define_singleton_method scope_name do |*values|
|
22
|
+
values = enumerized_attributes[name].find_values(*values).map(&:value)
|
23
|
+
values = values.first if values.size == 1
|
24
|
+
|
25
|
+
where(name => values)
|
26
|
+
end
|
27
|
+
|
28
|
+
if options[:scope] == true
|
29
|
+
define_singleton_method "without_#{name}" do |*values|
|
30
|
+
values = enumerized_attributes[name].find_values(*values).map(&:value)
|
31
|
+
where(arel_table[name].not_in(values))
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Enumerize
|
2
|
+
module Scope
|
3
|
+
module Mongoid
|
4
|
+
def enumerize(name, options={})
|
5
|
+
super
|
6
|
+
|
7
|
+
_enumerize_module.dependent_eval do
|
8
|
+
if self < ::Mongoid::Document
|
9
|
+
if options[:scope]
|
10
|
+
_define_scope_methods!(name, options)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def _define_scope_methods!(name, options)
|
19
|
+
scope_name = options[:scope] == true ? "with_#{name}" : options[:scope]
|
20
|
+
|
21
|
+
define_singleton_method scope_name do |*values|
|
22
|
+
values = enumerized_attributes[name].find_values(*values).map(&:value)
|
23
|
+
|
24
|
+
if values.size == 1
|
25
|
+
where(name => values.first)
|
26
|
+
else
|
27
|
+
where(name.in => values)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
if options[:scope] == true
|
32
|
+
define_singleton_method "without_#{name}" do |*values|
|
33
|
+
values = enumerized_attributes[name].find_values(*values).map(&:value)
|
34
|
+
not_in(name => values)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/enumerize/value.rb
CHANGED
@@ -17,6 +17,10 @@ module Enumerize
|
|
17
17
|
I18n.t(i18n_keys[0], :default => i18n_keys[1..-1])
|
18
18
|
end
|
19
19
|
|
20
|
+
def ==(other)
|
21
|
+
super(other.to_s) || value == other
|
22
|
+
end
|
23
|
+
|
20
24
|
def encode_with(coder)
|
21
25
|
coder.represent_object(self.class.superclass, @value)
|
22
26
|
end
|
@@ -36,7 +40,7 @@ module Enumerize
|
|
36
40
|
i18n_keys = i18n_scopes
|
37
41
|
i18n_keys << [:"enumerize.defaults.#{@attr.name}.#{self}"]
|
38
42
|
i18n_keys << [:"enumerize.#{@attr.name}.#{self}"]
|
39
|
-
i18n_keys << self.humanize # humanize value if there are no translations
|
43
|
+
i18n_keys << self.underscore.humanize # humanize value if there are no translations
|
40
44
|
i18n_keys.flatten
|
41
45
|
end
|
42
46
|
end
|
data/lib/enumerize/version.rb
CHANGED
data/test/activerecord_test.rb
CHANGED
@@ -242,6 +242,12 @@ describe Enumerize::ActiveRecordSupport do
|
|
242
242
|
User.having_role(:admin).must_equal [user_1]
|
243
243
|
end
|
244
244
|
|
245
|
+
it 'ignores not enumerized values that passed to the scope method' do
|
246
|
+
User.delete_all
|
247
|
+
|
248
|
+
User.with_status(:foo).must_equal []
|
249
|
+
end
|
250
|
+
|
245
251
|
it 'allows either key or value as valid' do
|
246
252
|
user_1 = User.new(status: :active)
|
247
253
|
user_2 = User.new(status: 1)
|
data/test/attribute_test.rb
CHANGED
@@ -85,6 +85,14 @@ describe Enumerize::Attribute do
|
|
85
85
|
end
|
86
86
|
end
|
87
87
|
|
88
|
+
it 'sets up shortcut methods for each value' do
|
89
|
+
build_attr nil, :foo, :in => {:a => 1, :b => 2}
|
90
|
+
attr.a.value.must_equal 1
|
91
|
+
attr.b.value.must_equal 2
|
92
|
+
attr.a.text.must_equal 'A'
|
93
|
+
attr.b.text.must_equal 'B'
|
94
|
+
end
|
95
|
+
|
88
96
|
describe 'values hash with zero' do
|
89
97
|
before do
|
90
98
|
build_attr nil, :foo, :in => {:a => 1, :b => 2, :c => 0}
|
@@ -99,6 +107,10 @@ describe Enumerize::Attribute do
|
|
99
107
|
attr.find_value(2).must_equal 'b'
|
100
108
|
attr.find_value(0).must_equal 'c'
|
101
109
|
end
|
110
|
+
|
111
|
+
it 'finds all values by hash values' do
|
112
|
+
attr.find_values(1, 2, 0).must_equal ['a', 'b', 'c']
|
113
|
+
end
|
102
114
|
end
|
103
115
|
|
104
116
|
describe 'boolean values hash' do
|
data/test/mongoid_test.rb
CHANGED
@@ -17,8 +17,8 @@ describe Enumerize do
|
|
17
17
|
|
18
18
|
field :sex
|
19
19
|
field :role
|
20
|
-
enumerize :sex, :in => %w[male female]
|
21
|
-
enumerize :role, :in => %w[admin user], :default => 'user'
|
20
|
+
enumerize :sex, :in => %w[male female], scope: true
|
21
|
+
enumerize :role, :in => %w[admin user], :default => 'user', scope: :having_role
|
22
22
|
enumerize :mult, :in => %w[one two three four], :multiple => true
|
23
23
|
end
|
24
24
|
|
@@ -76,4 +76,28 @@ describe Enumerize do
|
|
76
76
|
user = model.first
|
77
77
|
user.mult.to_a.must_equal ['one', 'two']
|
78
78
|
end
|
79
|
+
|
80
|
+
it 'adds scope' do
|
81
|
+
model.delete_all
|
82
|
+
|
83
|
+
user_1 = model.create!(sex: :male, role: :admin)
|
84
|
+
user_2 = model.create!(sex: :female, role: :user)
|
85
|
+
|
86
|
+
model.with_sex(:male).to_a.must_equal [user_1]
|
87
|
+
model.with_sex(:female).to_a.must_equal [user_2]
|
88
|
+
model.with_sex(:male, :female).to_set.must_equal [user_1, user_2].to_set
|
89
|
+
|
90
|
+
model.without_sex(:male).to_a.must_equal [user_2]
|
91
|
+
model.without_sex(:female).to_a.must_equal [user_1]
|
92
|
+
model.without_sex(:male, :female).to_a.must_equal []
|
93
|
+
|
94
|
+
model.having_role(:admin).to_a.must_equal [user_1]
|
95
|
+
model.having_role(:user).to_a.must_equal [user_2]
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'ignores not enumerized values that passed to the scope method' do
|
99
|
+
model.delete_all
|
100
|
+
|
101
|
+
model.with_sex(:foo).must_equal []
|
102
|
+
end
|
79
103
|
end
|
data/test/multiple_test.rb
CHANGED
@@ -33,4 +33,10 @@ describe Enumerize::Base do
|
|
33
33
|
klass.enumerize :foos, in: %w(a b c), multiple: true
|
34
34
|
object.wont_respond_to :foos_value
|
35
35
|
end
|
36
|
+
|
37
|
+
it "cannot define multiple with scope" do
|
38
|
+
assert_raises ArgumentError do
|
39
|
+
klass.enumerize :foos, in: %w(a b c), multiple: true, scope: true
|
40
|
+
end
|
41
|
+
end
|
36
42
|
end
|
data/test/value_test.rb
CHANGED
@@ -2,14 +2,27 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
describe Enumerize::Value do
|
4
4
|
let(:attr) { Struct.new(:values).new([]) }
|
5
|
-
let(:value) { Enumerize::Value.new(attr, 'test_value') }
|
5
|
+
let(:value) { Enumerize::Value.new(attr, 'test_value', 1) }
|
6
6
|
|
7
7
|
it 'is a string' do
|
8
8
|
value.must_be_kind_of String
|
9
9
|
end
|
10
10
|
|
11
|
-
|
12
|
-
|
11
|
+
describe 'equality' do
|
12
|
+
it 'is compared to string' do
|
13
|
+
value.must_be :==, 'test_value'
|
14
|
+
value.wont_be :==, 'not_value'
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'is compared to symbol' do
|
18
|
+
value.must_be :==, :test_value
|
19
|
+
value.wont_be :==, :not_value
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'is compared to integer' do
|
23
|
+
value.must_be :==, 1
|
24
|
+
value.wont_be :==, 2
|
25
|
+
end
|
13
26
|
end
|
14
27
|
|
15
28
|
describe 'translation' do
|
@@ -106,6 +119,8 @@ describe Enumerize::Value do
|
|
106
119
|
end
|
107
120
|
|
108
121
|
describe 'serialization' do
|
122
|
+
let(:value) { Enumerize::Value.new(attr, 'test_value') }
|
123
|
+
|
109
124
|
it 'should be serialized to yaml as string value' do
|
110
125
|
assert_equal YAML.dump('test_value'), YAML.dump(value)
|
111
126
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumerize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergey Nartimov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-02-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -56,6 +56,8 @@ files:
|
|
56
56
|
- lib/enumerize/module_attributes.rb
|
57
57
|
- lib/enumerize/predicatable.rb
|
58
58
|
- lib/enumerize/predicates.rb
|
59
|
+
- lib/enumerize/scope/activerecord.rb
|
60
|
+
- lib/enumerize/scope/mongoid.rb
|
59
61
|
- lib/enumerize/set.rb
|
60
62
|
- lib/enumerize/value.rb
|
61
63
|
- lib/enumerize/version.rb
|
@@ -97,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
99
|
version: '0'
|
98
100
|
requirements: []
|
99
101
|
rubyforge_project:
|
100
|
-
rubygems_version: 2.
|
102
|
+
rubygems_version: 2.4.5
|
101
103
|
signing_key:
|
102
104
|
specification_version: 4
|
103
105
|
summary: Enumerated attributes with I18n and ActiveRecord/Mongoid/MongoMapper support
|