mongoid-enum 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Guardfile +8 -0
- data/lib/mongoid/enum.rb +22 -3
- data/lib/mongoid/enum/version.rb +1 -1
- data/mongoid-enum.gemspec +4 -5
- data/spec/mongoid/enum_spec.rb +56 -1
- data/spec/spec_helper.rb +3 -7
- data/spec/support/mongoid.yml +1 -1
- metadata +12 -25
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: afeb11021cb69d6768757b3d64497ed210ea1385
|
4
|
+
data.tar.gz: d0ecaf60fdae16d33c695ad249fb9296fa455664
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 207868209b262b7fc9ab7420c276d46ff3b43d12b845ae811a0f3ae64934c82e4a61688920ad237f78a8bc0bab81d7477ae684da4b3dbfb962e054128a897ba7
|
7
|
+
data.tar.gz: 3e8224bf9a36c00f39d1b1a3123b25a8d241b79127dd9c94d20906e069d6dae0b695a238a80c202a0a9416bb16fe8bfdc073883e5825682641bf5ec6b5ecaf1c
|
data/Guardfile
ADDED
data/lib/mongoid/enum.rb
CHANGED
@@ -14,10 +14,10 @@ module Mongoid
|
|
14
14
|
set_values_constant name, values
|
15
15
|
|
16
16
|
create_field field_name, options
|
17
|
-
alias_attribute name, field_name
|
18
17
|
|
19
18
|
create_validations field_name, values, options
|
20
19
|
define_value_scopes_and_accessors field_name, values, options
|
20
|
+
define_field_accessor name, field_name, options
|
21
21
|
end
|
22
22
|
|
23
23
|
private
|
@@ -42,9 +42,10 @@ module Mongoid
|
|
42
42
|
|
43
43
|
def create_validations(field_name, values, options)
|
44
44
|
if options[:multiple] && options[:validate]
|
45
|
-
validates field_name, :'mongoid/enum/validators/multiple' => { :in => values, :allow_nil => !options[:required] }
|
45
|
+
validates field_name, :'mongoid/enum/validators/multiple' => { :in => values.map(&:to_sym), :allow_nil => !options[:required] }
|
46
|
+
#FIXME: Shouldn't this be `elsif options[:validate]` ???
|
46
47
|
elsif validate
|
47
|
-
validates field_name, :inclusion => {:in => values}, :allow_nil => !options[:required]
|
48
|
+
validates field_name, :inclusion => {:in => values.map(&:to_sym)}, :allow_nil => !options[:required]
|
48
49
|
end
|
49
50
|
end
|
50
51
|
|
@@ -60,6 +61,24 @@ module Mongoid
|
|
60
61
|
end
|
61
62
|
end
|
62
63
|
|
64
|
+
def define_field_accessor(name, field_name, options)
|
65
|
+
if options[:multiple]
|
66
|
+
define_array_field_accessor name, field_name
|
67
|
+
else
|
68
|
+
define_string_field_accessor name, field_name
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def define_array_field_accessor(name, field_name)
|
73
|
+
class_eval "def #{name}=(vals) self.write_attribute(:#{field_name}, Array(vals).compact.map(&:to_sym)) end"
|
74
|
+
class_eval "def #{name}() self.read_attribute(:#{field_name}) end"
|
75
|
+
end
|
76
|
+
|
77
|
+
def define_string_field_accessor(name, field_name)
|
78
|
+
class_eval "def #{name}=(val) self.write_attribute(:#{field_name}, val && val.to_sym || nil) end"
|
79
|
+
class_eval "def #{name}() self.read_attribute(:#{field_name}) end"
|
80
|
+
end
|
81
|
+
|
63
82
|
def define_array_accessor(field_name, value)
|
64
83
|
class_eval "def #{value}?() self.#{field_name}.include?(:#{value}) end"
|
65
84
|
class_eval "def #{value}!() update_attributes! :#{field_name} => (self.#{field_name} || []) + [:#{value}] end"
|
data/lib/mongoid/enum/version.rb
CHANGED
data/mongoid-enum.gemspec
CHANGED
@@ -18,12 +18,11 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_runtime_dependency "mongoid", "~>
|
21
|
+
spec.add_runtime_dependency "mongoid", "~> 5.0"
|
22
22
|
|
23
23
|
spec.add_development_dependency "bundler", "~> 1.3"
|
24
24
|
spec.add_development_dependency "rake"
|
25
|
-
spec.add_development_dependency "rspec", "~>
|
26
|
-
spec.add_development_dependency "guard-rspec", "~> 4.
|
27
|
-
spec.add_development_dependency "
|
28
|
-
spec.add_development_dependency "mongoid-rspec", "~> 1.5.1"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.1"
|
26
|
+
spec.add_development_dependency "guard-rspec", "~> 4.6.2"
|
27
|
+
spec.add_development_dependency "mongoid-rspec", "~> 3.0"
|
29
28
|
end
|
data/spec/mongoid/enum_spec.rb
CHANGED
@@ -42,7 +42,7 @@ describe Mongoid::Enum do
|
|
42
42
|
it "is aliased" do
|
43
43
|
expect(instance).to respond_to alias_name
|
44
44
|
expect(instance).to respond_to :"#{alias_name}="
|
45
|
-
expect(instance).to respond_to :"#{alias_name}
|
45
|
+
expect(instance).to respond_to :"#{alias_name}"
|
46
46
|
end
|
47
47
|
|
48
48
|
describe "type" do
|
@@ -68,6 +68,22 @@ describe Mongoid::Enum do
|
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
71
|
+
describe "'required' option" do
|
72
|
+
context "when true" do
|
73
|
+
let(:instance) { User.new status: nil }
|
74
|
+
it "is not valid with nil value" do
|
75
|
+
expect(instance).to_not be_valid
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context "when false" do
|
80
|
+
let(:instance) { User.new roles: nil }
|
81
|
+
it "is valid with nil value" do
|
82
|
+
expect(instance).to be_valid
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
71
87
|
describe "constant" do
|
72
88
|
it "is set to the values" do
|
73
89
|
expect(klass::STATUS).to eq values
|
@@ -76,6 +92,18 @@ describe Mongoid::Enum do
|
|
76
92
|
|
77
93
|
describe "accessors" do
|
78
94
|
context "when singular" do
|
95
|
+
describe "setter" do
|
96
|
+
it "accepts strings" do
|
97
|
+
instance.status = 'banned'
|
98
|
+
expect(instance.status).to eq :banned
|
99
|
+
end
|
100
|
+
|
101
|
+
it "accepts symbols" do
|
102
|
+
instance.status = :banned
|
103
|
+
expect(instance.status).to eq :banned
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
79
107
|
describe "{{value}}!" do
|
80
108
|
it "sets the value" do
|
81
109
|
instance.save
|
@@ -103,6 +131,33 @@ describe Mongoid::Enum do
|
|
103
131
|
end
|
104
132
|
|
105
133
|
context "when multiple" do
|
134
|
+
describe "setter" do
|
135
|
+
it "accepts strings" do
|
136
|
+
instance.roles = "author"
|
137
|
+
expect(instance.roles).to eq [:author]
|
138
|
+
end
|
139
|
+
|
140
|
+
it "accepts symbols" do
|
141
|
+
instance.roles = :author
|
142
|
+
expect(instance.roles).to eq [:author]
|
143
|
+
end
|
144
|
+
|
145
|
+
it "accepts arrays of strings" do
|
146
|
+
instance.roles = ['author', 'editor']
|
147
|
+
instance.save
|
148
|
+
puts instance.errors.full_messages
|
149
|
+
instance.reload
|
150
|
+
expect(instance.roles).to include(:author)
|
151
|
+
expect(instance.roles).to include(:editor)
|
152
|
+
end
|
153
|
+
|
154
|
+
it "accepts arrays of symbols" do
|
155
|
+
instance.roles = [:author, :editor]
|
156
|
+
expect(instance.roles).to include(:author)
|
157
|
+
expect(instance.roles).to include(:editor)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
106
161
|
describe "{{value}}!" do
|
107
162
|
context "when field is nil" do
|
108
163
|
it "creates an array containing the value" do
|
data/spec/spec_helper.rb
CHANGED
@@ -1,22 +1,18 @@
|
|
1
1
|
$: << File.expand_path("../../lib", __FILE__)
|
2
2
|
|
3
|
-
require 'database_cleaner'
|
4
3
|
require 'mongoid'
|
5
|
-
require
|
4
|
+
require "mongoid/rspec"
|
6
5
|
require 'mongoid/enum'
|
7
6
|
|
8
7
|
ENV['MONGOID_ENV'] = "test"
|
9
8
|
|
10
9
|
RSpec.configure do |config|
|
11
10
|
config.include Mongoid::Matchers
|
12
|
-
config.before(:suite) do
|
13
|
-
DatabaseCleaner.strategy = :truncation
|
14
|
-
end
|
15
11
|
|
16
|
-
config.
|
17
|
-
DatabaseCleaner.clean
|
12
|
+
config.before(:each) do
|
18
13
|
Mongoid.purge!
|
19
14
|
end
|
20
15
|
end
|
21
16
|
|
22
17
|
Mongoid.load!(File.expand_path("../support/mongoid.yml", __FILE__), :test)
|
18
|
+
Mongo::Logger.logger.level = ::Logger::INFO
|
data/spec/support/mongoid.yml
CHANGED
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: 0.
|
4
|
+
version: 0.4.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: 2015-
|
11
|
+
date: 2015-11-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '5.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
|
-
version: '
|
26
|
+
version: '5.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -58,56 +58,42 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '3.1'
|
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
|
-
version: '
|
68
|
+
version: '3.1'
|
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
|
-
version: 4.
|
75
|
+
version: 4.6.2
|
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
|
-
version: 4.
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: database_cleaner
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
86
|
-
requirements:
|
87
|
-
- - "~>"
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: 1.2.0
|
90
|
-
type: :development
|
91
|
-
prerelease: false
|
92
|
-
version_requirements: !ruby/object:Gem::Requirement
|
93
|
-
requirements:
|
94
|
-
- - "~>"
|
95
|
-
- !ruby/object:Gem::Version
|
96
|
-
version: 1.2.0
|
82
|
+
version: 4.6.2
|
97
83
|
- !ruby/object:Gem::Dependency
|
98
84
|
name: mongoid-rspec
|
99
85
|
requirement: !ruby/object:Gem::Requirement
|
100
86
|
requirements:
|
101
87
|
- - "~>"
|
102
88
|
- !ruby/object:Gem::Version
|
103
|
-
version:
|
89
|
+
version: '3.0'
|
104
90
|
type: :development
|
105
91
|
prerelease: false
|
106
92
|
version_requirements: !ruby/object:Gem::Requirement
|
107
93
|
requirements:
|
108
94
|
- - "~>"
|
109
95
|
- !ruby/object:Gem::Version
|
110
|
-
version:
|
96
|
+
version: '3.0'
|
111
97
|
description: Heavily inspired by DDH's ActiveRecord::Enum, this little library is
|
112
98
|
there to help you cut down the cruft in your models and make the world a happier
|
113
99
|
place at the same time.
|
@@ -122,6 +108,7 @@ files:
|
|
122
108
|
- ".rspec"
|
123
109
|
- ".travis.yml"
|
124
110
|
- Gemfile
|
111
|
+
- Guardfile
|
125
112
|
- LICENSE.txt
|
126
113
|
- README.md
|
127
114
|
- Rakefile
|
@@ -155,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
155
142
|
version: '0'
|
156
143
|
requirements: []
|
157
144
|
rubyforge_project:
|
158
|
-
rubygems_version: 2.4.
|
145
|
+
rubygems_version: 2.4.5
|
159
146
|
signing_key:
|
160
147
|
specification_version: 4
|
161
148
|
summary: Sweet enum sugar for your Mongoid documents
|