mongoid-enum 0.2.0 → 0.3.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 +5 -5
- data/lib/mongoid/enum.rb +2 -1
- data/lib/mongoid/enum/configuration.rb +19 -0
- data/lib/mongoid/enum/version.rb +1 -1
- data/spec/mongoid/configuration_spec.rb +11 -0
- data/spec/mongoid/enum/validators/multiple_validator_spec.rb +8 -8
- data/spec/mongoid/enum_spec.rb +39 -4
- metadata +26 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fe0e5157d30b2aac72047e14db8cca549db38f29
|
4
|
+
data.tar.gz: ff1af2e59142bd12feaa332998d0dbbffa89e10a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6794021fe88958a5746395f4a87ce1f26cf5ef01977578ab6212c877f9e9184086beed7de31f0465a389fd0f74512f08d7488dad8aa9790d28c52d4168a737e3
|
7
|
+
data.tar.gz: 0f83fdf80128ed57c9c5229c31fbd4bd2e7803e809e6652ee35d1fb74b237ed38c4aa22b6521b9ae0ebf9593ee0f2b367686962979b19771daa86c845ae45476
|
data/lib/mongoid/enum.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require "mongoid/enum/version"
|
2
2
|
require "mongoid/enum/validators/multiple_validator"
|
3
|
+
require "mongoid/enum/configuration"
|
3
4
|
|
4
5
|
module Mongoid
|
5
6
|
module Enum
|
@@ -7,7 +8,7 @@ module Mongoid
|
|
7
8
|
module ClassMethods
|
8
9
|
|
9
10
|
def enum(name, values, options = {})
|
10
|
-
field_name = :"
|
11
|
+
field_name = :"#{Mongoid::Enum.configuration.field_name_prefix}#{name}"
|
11
12
|
options = default_options(values).merge(options)
|
12
13
|
|
13
14
|
set_values_constant name, values
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Enum
|
3
|
+
class Configuration
|
4
|
+
attr_accessor :field_name_prefix
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
self.field_name_prefix = "_"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.configuration
|
12
|
+
@configuration ||= Configuration.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.configure
|
16
|
+
yield(configuration) if block_given?
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/mongoid/enum/version.rb
CHANGED
@@ -16,14 +16,14 @@ describe Mongoid::Enum::Validators::MultipleValidator do
|
|
16
16
|
context "and value is nil" do
|
17
17
|
before(:each) { validator.validate_each(record, attribute, nil) }
|
18
18
|
it "validates" do
|
19
|
-
expect(record.errors[attribute].empty?).to
|
19
|
+
expect(record.errors[attribute].empty?).to be true
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
23
|
context "and value is []" do
|
24
24
|
before(:each) { validator.validate_each(record, attribute, []) }
|
25
25
|
it "validates" do
|
26
|
-
expect(record.errors[attribute].empty?).to
|
26
|
+
expect(record.errors[attribute].empty?).to be true
|
27
27
|
end
|
28
28
|
end
|
29
29
|
end
|
@@ -32,14 +32,14 @@ describe Mongoid::Enum::Validators::MultipleValidator do
|
|
32
32
|
context "and value is nil" do
|
33
33
|
before(:each) { validator.validate_each(record, attribute, nil) }
|
34
34
|
it "won't validate" do
|
35
|
-
expect(record.errors[attribute].any?).to
|
35
|
+
expect(record.errors[attribute].any?).to be true
|
36
36
|
expect(record.errors[attribute]).to eq ["is not in #{values.join ", "}"]
|
37
37
|
end
|
38
38
|
end
|
39
39
|
context "and value is []" do
|
40
40
|
before(:each) { validator.validate_each(record, attribute, []) }
|
41
41
|
it "won't validate" do
|
42
|
-
expect(record.errors[attribute].any?).to
|
42
|
+
expect(record.errors[attribute].any?).to be true
|
43
43
|
expect(record.errors[attribute]).to eq ["is not in #{values.join ", "}"]
|
44
44
|
end
|
45
45
|
end
|
@@ -49,7 +49,7 @@ describe Mongoid::Enum::Validators::MultipleValidator do
|
|
49
49
|
let(:allow_nil) { rand(2).zero? }
|
50
50
|
before(:each) { validator.validate_each(record, attribute, [values.sample]) }
|
51
51
|
it "validates" do
|
52
|
-
expect(record.errors[attribute].empty?).to
|
52
|
+
expect(record.errors[attribute].empty?).to be true
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
@@ -57,7 +57,7 @@ describe Mongoid::Enum::Validators::MultipleValidator do
|
|
57
57
|
let(:allow_nil) { rand(2).zero? }
|
58
58
|
before(:each) { validator.validate_each(record, attribute, [:amet]) }
|
59
59
|
it "won't validate" do
|
60
|
-
expect(record.errors[attribute].any?).to
|
60
|
+
expect(record.errors[attribute].any?).to be true
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
@@ -65,7 +65,7 @@ describe Mongoid::Enum::Validators::MultipleValidator do
|
|
65
65
|
let(:allow_nil) { rand(2).zero? }
|
66
66
|
before(:each) { validator.validate_each(record, attribute, [values.first, values.last]) }
|
67
67
|
it "validates" do
|
68
|
-
expect(record.errors[attribute].empty?).to
|
68
|
+
expect(record.errors[attribute].empty?).to be true
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
@@ -73,7 +73,7 @@ describe Mongoid::Enum::Validators::MultipleValidator do
|
|
73
73
|
let(:allow_nil) { rand(2).zero? }
|
74
74
|
before(:each) { validator.validate_each(record, attribute, [values.first, values.last, :amet]) }
|
75
75
|
it "won't validate" do
|
76
|
-
expect(record.errors[attribute].any?).to
|
76
|
+
expect(record.errors[attribute].any?).to be true
|
77
77
|
end
|
78
78
|
end
|
79
79
|
end
|
data/spec/mongoid/enum_spec.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'mongoid/enum/configuration'
|
2
3
|
|
3
4
|
class User
|
4
5
|
include Mongoid::Document
|
@@ -21,6 +22,23 @@ describe Mongoid::Enum do
|
|
21
22
|
expect(klass).to have_field(field_name)
|
22
23
|
end
|
23
24
|
|
25
|
+
it "uses prefix defined in configuration" do
|
26
|
+
old_field_name_prefix = Mongoid::Enum.configuration.field_name_prefix
|
27
|
+
Mongoid::Enum.configure do |config|
|
28
|
+
config.field_name_prefix = "___"
|
29
|
+
end
|
30
|
+
UserWithoutPrefix = Class.new do
|
31
|
+
include Mongoid::Document
|
32
|
+
include Mongoid::Enum
|
33
|
+
|
34
|
+
enum :status, [:awaiting_approval, :approved, :banned]
|
35
|
+
end
|
36
|
+
expect(UserWithoutPrefix).to have_field "___status"
|
37
|
+
Mongoid::Enum.configure do |config|
|
38
|
+
config.field_name_prefix = old_field_name_prefix
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
24
42
|
it "is aliased" do
|
25
43
|
expect(instance).to respond_to alias_name
|
26
44
|
expect(instance).to respond_to :"#{alias_name}="
|
@@ -56,7 +74,7 @@ describe Mongoid::Enum do
|
|
56
74
|
end
|
57
75
|
end
|
58
76
|
|
59
|
-
describe "accessors"do
|
77
|
+
describe "accessors" do
|
60
78
|
context "when singular" do
|
61
79
|
describe "{{value}}!" do
|
62
80
|
it "sets the value" do
|
@@ -111,15 +129,15 @@ describe Mongoid::Enum do
|
|
111
129
|
instance.save
|
112
130
|
instance.author!
|
113
131
|
instance.editor!
|
114
|
-
expect(instance.editor?).to
|
115
|
-
expect(instance.author?).to
|
132
|
+
expect(instance.editor?).to be true
|
133
|
+
expect(instance.author?).to be true
|
116
134
|
end
|
117
135
|
end
|
118
136
|
|
119
137
|
context "when {{enum}} does not contain {{value}}" do
|
120
138
|
it "returns false" do
|
121
139
|
instance.save
|
122
|
-
expect(instance.author?).to
|
140
|
+
expect(instance.author?).to be false
|
123
141
|
end
|
124
142
|
end
|
125
143
|
end
|
@@ -174,4 +192,21 @@ describe Mongoid::Enum do
|
|
174
192
|
end
|
175
193
|
end
|
176
194
|
end
|
195
|
+
|
196
|
+
describe ".configuration" do
|
197
|
+
it "returns Configuration object" do
|
198
|
+
expect(Mongoid::Enum.configuration)
|
199
|
+
.to be_instance_of Mongoid::Enum::Configuration
|
200
|
+
end
|
201
|
+
it "returns same object when called multiple times" do
|
202
|
+
expect(Mongoid::Enum.configuration).to be Mongoid::Enum.configuration
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
describe ".configure" do
|
207
|
+
it "yields configuration if block is given" do
|
208
|
+
expect { |b| Mongoid::Enum.configure &b }
|
209
|
+
.to yield_with_args Mongoid::Enum.configuration
|
210
|
+
end
|
211
|
+
end
|
177
212
|
end
|
metadata
CHANGED
@@ -1,111 +1,111 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nicholas Bruning
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-07-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '4.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '4.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '1.3'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - ~>
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1.3'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - ~>
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '2.14'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - ~>
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '2.14'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: guard-rspec
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - ~>
|
73
|
+
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: 4.0.3
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - ~>
|
80
|
+
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: 4.0.3
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: database_cleaner
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- - ~>
|
87
|
+
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: 1.2.0
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- - ~>
|
94
|
+
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: 1.2.0
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: mongoid-rspec
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
|
-
- - ~>
|
101
|
+
- - "~>"
|
102
102
|
- !ruby/object:Gem::Version
|
103
103
|
version: 1.5.1
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
|
-
- - ~>
|
108
|
+
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: 1.5.1
|
111
111
|
description: Heavily inspired by DDH's ActiveRecord::Enum, this little library is
|
@@ -117,18 +117,20 @@ executables: []
|
|
117
117
|
extensions: []
|
118
118
|
extra_rdoc_files: []
|
119
119
|
files:
|
120
|
-
- .autotest
|
121
|
-
- .gitignore
|
122
|
-
- .rspec
|
123
|
-
- .travis.yml
|
120
|
+
- ".autotest"
|
121
|
+
- ".gitignore"
|
122
|
+
- ".rspec"
|
123
|
+
- ".travis.yml"
|
124
124
|
- Gemfile
|
125
125
|
- LICENSE.txt
|
126
126
|
- README.md
|
127
127
|
- Rakefile
|
128
128
|
- lib/mongoid/enum.rb
|
129
|
+
- lib/mongoid/enum/configuration.rb
|
129
130
|
- lib/mongoid/enum/validators/multiple_validator.rb
|
130
131
|
- lib/mongoid/enum/version.rb
|
131
132
|
- mongoid-enum.gemspec
|
133
|
+
- spec/mongoid/configuration_spec.rb
|
132
134
|
- spec/mongoid/enum/validators/multiple_validator_spec.rb
|
133
135
|
- spec/mongoid/enum_spec.rb
|
134
136
|
- spec/spec_helper.rb
|
@@ -143,21 +145,22 @@ require_paths:
|
|
143
145
|
- lib
|
144
146
|
required_ruby_version: !ruby/object:Gem::Requirement
|
145
147
|
requirements:
|
146
|
-
- -
|
148
|
+
- - ">="
|
147
149
|
- !ruby/object:Gem::Version
|
148
150
|
version: '0'
|
149
151
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
150
152
|
requirements:
|
151
|
-
- -
|
153
|
+
- - ">="
|
152
154
|
- !ruby/object:Gem::Version
|
153
155
|
version: '0'
|
154
156
|
requirements: []
|
155
157
|
rubyforge_project:
|
156
|
-
rubygems_version: 2.
|
158
|
+
rubygems_version: 2.4.6
|
157
159
|
signing_key:
|
158
160
|
specification_version: 4
|
159
161
|
summary: Sweet enum sugar for your Mongoid documents
|
160
162
|
test_files:
|
163
|
+
- spec/mongoid/configuration_spec.rb
|
161
164
|
- spec/mongoid/enum/validators/multiple_validator_spec.rb
|
162
165
|
- spec/mongoid/enum_spec.rb
|
163
166
|
- spec/spec_helper.rb
|