mongoid_enum 1.0.1 → 1.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7322c753d62aa63f83857466b28c7893b25a813d
4
- data.tar.gz: 833b534f7c445d679d7765d3298b42b1a26da430
3
+ metadata.gz: 7ac0771a33322e56736c4377c96af138033bd23a
4
+ data.tar.gz: 404023e3a923cce6ac08aee8a88fe13f522b26a2
5
5
  SHA512:
6
- metadata.gz: e0c1bf3e5d739ec44ff977f702d5c5deb52b45fba80f5ae8dbe0ed35f08db020cb0679363cb7ea6d5d0d4747661873d6a3ee39df19b5cf0663aa1aa6c4687132
7
- data.tar.gz: a98fb0dfd5d5b458f1d0b4079680e6810c241c00704da5a4d87156456f344097bfc5b7a74bac2bddb39c5200bc26a1d9cca229f5118a1c89b9b3b5c4490cb835
6
+ metadata.gz: b1f86793cab3fa90274bbf9f9c8018758ffcbfa4a886e00d1c144669d6605af22fed744b23918d33950a33bc10fca7006357c7bff7e0f2ba45e7f3f5209bfb29
7
+ data.tar.gz: eb1dd4b059b0b5f55ac8c57a21662d48111faaab8f33f5c25cec189b0340854c4b6bde6ec738d61d1a4e7bb028dbede175ccb8afb8377460ffe2f94b9a6d94ff
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
- # Introduction
1
+ # Mongoid::Enum
2
+
3
+ [![Build Status](https://travis-ci.org/amw/mongoid_enum.svg?branch=master)](https://travis-ci.org/amw/mongoid_enum)
2
4
 
3
5
  Enum implementation for Mongoid. Similar to that found in ActiveRecord.
4
6
 
@@ -52,6 +54,17 @@ Constant name is a pluralized field name set in SCREAMING\_SNAKE\_CASE.
52
54
  Part::QUALITY_CONTROLS["passed"] # true
53
55
  Part::QUALITY_CONTROLS[:failed] # false
54
56
 
57
+ You might prefer to use plural scopes if your field values are nouns:
58
+
59
+ class Attachment
60
+ include Mongoid::Document
61
+ include Mongoid::Enum
62
+
63
+ enum type: %w{image video}, _plural_scopes: true
64
+ end
65
+
66
+ Attachment.videos.count
67
+
55
68
  Read more in [documentation](http://www.rubydoc.info/gems/mongoid_enum/Mongoid/Enum).
56
69
 
57
70
  # Differences from ActiveRecord
@@ -57,6 +57,17 @@ module Mongoid # :nodoc:
57
57
  # Conversation.where(status: [:active, :archived])
58
58
  # Conversation.not.where(status: :active)
59
59
  #
60
+ # If your field values are nouns you might prefer to have your scopes pluralized:
61
+ #
62
+ # class Attachment
63
+ # include Mongoid::Document
64
+ # include Mongoid::Enum
65
+ #
66
+ # enum type: %w{image video}, _plural_scopes: true
67
+ # end
68
+ #
69
+ # Attachment.videos.count
70
+ #
60
71
  #
61
72
  # Defining an enum automatically adds a validator on its field. Assigning values
62
73
  # not included in enum definition will make the document invalid.
@@ -133,6 +144,7 @@ module Mongoid # :nodoc:
133
144
  enum_prefix = definitions.delete(:_prefix)
134
145
  enum_suffix = definitions.delete(:_suffix)
135
146
  default_key = definitions.delete(:_default)
147
+ pluralize = definitions.delete(:_plural_scopes)
136
148
 
137
149
  definitions.each do |name, values|
138
150
  enum_values = ActiveSupport::HashWithIndifferentAccess.new
@@ -187,6 +199,7 @@ module Mongoid # :nodoc:
187
199
  end
188
200
 
189
201
  value_method_name = "#{prefix}#{key}#{suffix}"
202
+ scope_name = pluralize ? value_method_name.pluralize : value_method_name
190
203
 
191
204
  # def active?() status == 0 end
192
205
  klass.send(:detect_enum_conflict!, name, "#{value_method_name}?")
@@ -197,8 +210,8 @@ module Mongoid # :nodoc:
197
210
  define_method("#{value_method_name}!") { update! name => key }
198
211
 
199
212
  # scope :active, -> { where status: 0 }
200
- klass.send(:detect_enum_conflict!, name, value_method_name, true)
201
- klass.scope value_method_name, -> { klass.where name => key }
213
+ klass.send(:detect_enum_conflict!, name, scope_name, true)
214
+ klass.scope scope_name, -> { klass.where name => key }
202
215
  end
203
216
  end
204
217
 
@@ -13,7 +13,7 @@ module Mongoid
13
13
  end
14
14
 
15
15
  def raise_error
16
- raise InvalidKeyError, "invalid enum key: #{original_key}"
16
+ fail InvalidKeyError, "invalid enum key: #{original_key}"
17
17
  end
18
18
  alias_method :bson_type, :raise_error
19
19
  alias_method :to_bson, :raise_error
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.1
4
+ version: 1.1.0
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-23 00:00:00.000000000 Z
11
+ date: 2016-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
@@ -104,9 +104,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
104
  version: '0'
105
105
  requirements: []
106
106
  rubyforge_project:
107
- rubygems_version: 2.4.5.1
107
+ rubygems_version: 2.5.1
108
108
  signing_key:
109
109
  specification_version: 4
110
110
  summary: Enum fields for Mongoid
111
111
  test_files: []
112
- has_rdoc: