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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -0
  3. data/lib/mongoid/enum.rb +26 -28
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b67dcfd5fb5d061dd20797d9ebe7dc54114030d2
4
- data.tar.gz: 982b2d073dd01bb563f8446b05f37a9e30dee7a7
3
+ metadata.gz: 7322c753d62aa63f83857466b28c7893b25a813d
4
+ data.tar.gz: 833b534f7c445d679d7765d3298b42b1a26da430
5
5
  SHA512:
6
- metadata.gz: 19171e4d95f8c56c387306f2745af4a685ac3122e5d9804dfe9bc1db84866a32fdde3fa90f5d2805abe8a6522f9d44d2d63122c94739412a24378bccbd92f13d
7
- data.tar.gz: 0732be4b09f52e3b8a6b3896d52c61b51aa5fbdcf31a3d9212ca7b26e7bc92e8f06733c22b30a251025119b5150309981a638d5ea23c91122c025cb16cd55ebe
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
- # Conversation.where(status: [:active, :archived])
39
- # Conversation.not.where(status: :active)
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: [ :active, :archived ], _default: :active
38
+ # enum status: { active: 0, archived: 1 }, _default: :active
48
39
  # end
49
40
  #
50
- # Finally, it's also possible to explicitly map the relation between attribute and
51
- # values stored in the database with a hash. The values don't even need to be Strings.
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
- # class Conversation
55
- # include Mongoid::Document
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
- # The mappings are exposed through a class constant with the pluralized attribute
62
- # name. It defines the mapping in a +HashWithIndifferentAccess+:
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? # true
114
+ # part.qc_pending? # true
115
+ # part["quality_control"] # nil
116
+ # part.quality_control # "pending"
120
117
  # part.qc_passed!
121
- # part.qc_pending? # false
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.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-22 00:00:00.000000000 Z
11
+ date: 2015-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid