mongoid_enum 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -0
- data/lib/mongoid/enum.rb +26 -28
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7322c753d62aa63f83857466b28c7893b25a813d
|
4
|
+
data.tar.gz: 833b534f7c445d679d7765d3298b42b1a26da430
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e0c1bf3e5d739ec44ff977f702d5c5deb52b45fba80f5ae8dbe0ed35f08db020cb0679363cb7ea6d5d0d4747661873d6a3ee39df19b5cf0663aa1aa6c4687132
|
7
|
+
data.tar.gz: a98fb0dfd5d5b458f1d0b4079680e6810c241c00704da5a4d87156456f344097bfc5b7a74bac2bddb39c5200bc26a1d9cca229f5118a1c89b9b3b5c4490cb835
|
data/README.md
CHANGED
@@ -34,6 +34,8 @@ the enum mapping:
|
|
34
34
|
end
|
35
35
|
|
36
36
|
part = Part.qc_pending.first
|
37
|
+
part.qc_pending? # true
|
38
|
+
part["quality_control"] # nil
|
37
39
|
part.quality_control # "pending"
|
38
40
|
part.qc_passed!
|
39
41
|
part.quality_control # "passed"
|
@@ -50,6 +52,8 @@ Constant name is a pluralized field name set in SCREAMING\_SNAKE\_CASE.
|
|
50
52
|
Part::QUALITY_CONTROLS["passed"] # true
|
51
53
|
Part::QUALITY_CONTROLS[:failed] # false
|
52
54
|
|
55
|
+
Read more in [documentation](http://www.rubydoc.info/gems/mongoid_enum/Mongoid/Enum).
|
56
|
+
|
53
57
|
# Differences from ActiveRecord
|
54
58
|
|
55
59
|
1. Default values are strings, not integers. I think it fits MongoDB better.
|
data/lib/mongoid/enum.rb
CHANGED
@@ -26,43 +26,37 @@ module Mongoid # :nodoc:
|
|
26
26
|
# conversation.status.nil? # => true
|
27
27
|
# conversation.status # => nil
|
28
28
|
#
|
29
|
-
# Scopes based on the allowed values of the enum field will be provided
|
30
|
-
# as well. With the above example:
|
31
|
-
#
|
32
|
-
# Conversation.active
|
33
|
-
# Conversation.archived
|
34
|
-
#
|
35
|
-
# Of course, you can also query them directly if the scopes don't fit your
|
36
|
-
# needs:
|
37
29
|
#
|
38
|
-
#
|
39
|
-
#
|
40
|
-
#
|
41
|
-
# You can set the default value:
|
30
|
+
# By default the whole label name is saved in the database as string, but you can
|
31
|
+
# explicitly declare values for each label. Strings, numbers, booleans, nil and some
|
32
|
+
# others types are allowed.
|
42
33
|
#
|
43
34
|
# class Conversation
|
44
35
|
# include Mongoid::Document
|
45
36
|
# include Mongoid::Enum
|
46
37
|
#
|
47
|
-
# enum status:
|
38
|
+
# enum status: { active: 0, archived: 1 }, _default: :active
|
48
39
|
# end
|
49
40
|
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
# Numbers, Booleans, nil and some others are allowed.
|
41
|
+
# The mappings are exposed through a class constant with the pluralized field name.
|
42
|
+
# It defines the mapping using +HashWithIndifferentAccess+:
|
53
43
|
#
|
54
|
-
#
|
55
|
-
#
|
56
|
-
# include Mongoid::Enum
|
44
|
+
# Conversation::STATUSES[:active] # => 0
|
45
|
+
# Conversation::STATUSES["archived"] # => 1
|
57
46
|
#
|
58
|
-
# enum status: { active: 0, archived: 1 }
|
59
|
-
# end
|
60
47
|
#
|
61
|
-
#
|
62
|
-
#
|
48
|
+
# Scopes based on the allowed values of the enum field will be provided
|
49
|
+
# as well. With the above example:
|
50
|
+
#
|
51
|
+
# Conversation.active
|
52
|
+
# Conversation.archived
|
53
|
+
#
|
54
|
+
# Of course, you can also query them directly if the scopes don't fit your
|
55
|
+
# needs:
|
56
|
+
#
|
57
|
+
# Conversation.where(status: [:active, :archived])
|
58
|
+
# Conversation.not.where(status: :active)
|
63
59
|
#
|
64
|
-
# Conversation::STATUSES[:active] # => 0
|
65
|
-
# Conversation::STATUSES["archived"] # => 1
|
66
60
|
#
|
67
61
|
# Defining an enum automatically adds a validator on its field. Assigning values
|
68
62
|
# not included in enum definition will make the document invalid.
|
@@ -78,7 +72,7 @@ module Mongoid # :nodoc:
|
|
78
72
|
# include Mongoid::Document
|
79
73
|
# include Mongoid::Enum
|
80
74
|
#
|
81
|
-
# enum status: { active: 0, archived: 1 }
|
75
|
+
# enum status: { active: 0, archived: 1 }, _default: :active
|
82
76
|
#
|
83
77
|
# validates :status, presence: true
|
84
78
|
# end
|
@@ -106,6 +100,7 @@ module Mongoid # :nodoc:
|
|
106
100
|
# conversation.comments_inactive!
|
107
101
|
# conversation.comments_active? # => false
|
108
102
|
#
|
103
|
+
#
|
109
104
|
# If you want to you can give nil value an explicit label.
|
110
105
|
#
|
111
106
|
# class Part
|
@@ -116,9 +111,12 @@ module Mongoid # :nodoc:
|
|
116
111
|
# end
|
117
112
|
#
|
118
113
|
# part = Part.qc_pending.first
|
119
|
-
# part.qc_pending?
|
114
|
+
# part.qc_pending? # true
|
115
|
+
# part["quality_control"] # nil
|
116
|
+
# part.quality_control # "pending"
|
120
117
|
# part.qc_passed!
|
121
|
-
# part.
|
118
|
+
# part.quality_control # "passed"
|
119
|
+
# part["quality_control"] # true
|
122
120
|
module Enum
|
123
121
|
extend ActiveSupport::Concern
|
124
122
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Wróbel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|