hipaa-mongoid-enum 0.5.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 +7 -0
- data/.autotest +0 -0
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/.travis.yml +14 -0
- data/Gemfile +4 -0
- data/Guardfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +168 -0
- data/Rakefile +1 -0
- data/lib/mongoid/enum.rb +93 -0
- data/lib/mongoid/enum/configuration.rb +19 -0
- data/lib/mongoid/enum/validators/multiple_validator.rb +29 -0
- data/lib/mongoid/enum/version.rb +5 -0
- data/mongoid-enum.gemspec +30 -0
- data/spec/mongoid/configuration_spec.rb +11 -0
- data/spec/mongoid/enum/validators/multiple_validator_spec.rb +80 -0
- data/spec/mongoid/enum_spec.rb +267 -0
- data/spec/spec_helper.rb +18 -0
- data/spec/support/mongoid.yml +6 -0
- metadata +154 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a94784740b28645b9815ab046af7af0c0e759450
|
4
|
+
data.tar.gz: 29ffa36443617787d100e14514a58bb401dcb6b0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 881451b0b9ca12c2f54553efc91f9cb063d105b144748608b5b4db4528d7945c415a9f3eaaba8a245ce5ad9260a99fa6f400c4ac8ec97d8ca0539534e61bc093
|
7
|
+
data.tar.gz: 0edf0b2ab695c6e718142627fef67ad6a6f3dad9d83716c4c34bd4a71e4260ade141d14efef24475153835001a290740e7f5e757ea685a3edd75c3c802514ee1
|
data/.autotest
ADDED
File without changes
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Nicholas Bruning
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,168 @@
|
|
1
|
+
# Mongoid::Enum
|
2
|
+
|
3
|
+
[](https://travis-ci.org/thetron/mongoid-enum)
|
5
|
+
[](https://codeclimate.com/github/thetron/mongoid-enum)
|
6
|
+
|
7
|
+
Heavily inspired by [DHH's
|
8
|
+
ActiveRecord::Enum](https://github.com/rails/rails/commit/db41eb8a6ea88b854bf5cd11070ea4245e1639c5), this little library is
|
9
|
+
there to help you cut down the cruft in your models and make the
|
10
|
+
world a happier place at the same time.
|
11
|
+
|
12
|
+
A single line will get you fields, accessors, validations and scopes,
|
13
|
+
and a few other bits-and-bobs.
|
14
|
+
|
15
|
+
|
16
|
+
# Installation
|
17
|
+
|
18
|
+
Add this to your Gemfile:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
gem "mongoid-enum"
|
22
|
+
```
|
23
|
+
|
24
|
+
And then run `bundle install`.
|
25
|
+
|
26
|
+
|
27
|
+
# Usage
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class Payment
|
31
|
+
include Mongoid::Document
|
32
|
+
include Mongoid::Enum
|
33
|
+
|
34
|
+
enum :status, [:pending, :approved, :declined]
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
Aaaaaaand then you get things like:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
payment = Payment.create
|
42
|
+
|
43
|
+
payment.status
|
44
|
+
# => :pending
|
45
|
+
|
46
|
+
payment.approved!
|
47
|
+
# => :approved
|
48
|
+
|
49
|
+
payment.pending?
|
50
|
+
# => :false
|
51
|
+
|
52
|
+
Payment.approved
|
53
|
+
# => Mongoid::Criteria for payments with status == :approved
|
54
|
+
|
55
|
+
```
|
56
|
+
|
57
|
+
# Features
|
58
|
+
|
59
|
+
## Field
|
60
|
+
|
61
|
+
Your enum value is stored as either a Symbol, or an Array (when storing
|
62
|
+
multiple values). The actual field name has a leading underscore (e.g.:
|
63
|
+
`_status`), but is also aliased with its actual name for you
|
64
|
+
convenience.
|
65
|
+
|
66
|
+
|
67
|
+
## Accessors
|
68
|
+
|
69
|
+
Your enums will get getters-and-setters with the same name. So using the
|
70
|
+
`Payment` example above:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
payment.status = :declined
|
74
|
+
payment.status
|
75
|
+
# => :declined
|
76
|
+
```
|
77
|
+
|
78
|
+
And you also get bang(!) and query(?) methods for each of the values in
|
79
|
+
your enum (see [this example](#usage).
|
80
|
+
|
81
|
+
|
82
|
+
## Constants
|
83
|
+
|
84
|
+
For each enum, you'll also get a constant named after it. This is to
|
85
|
+
help you elsewhere in your app, should you need to display, or leverage
|
86
|
+
the list of values. Using the above example:
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
Payment::STATUS
|
90
|
+
# => [:pending, :approved, :declined]
|
91
|
+
```
|
92
|
+
|
93
|
+
|
94
|
+
## Validations
|
95
|
+
|
96
|
+
Enum values are automatically validated against the list. You can
|
97
|
+
disable this behaviour (see [below](#options)).
|
98
|
+
|
99
|
+
|
100
|
+
## Scopes
|
101
|
+
|
102
|
+
A scope added for each of your enum's values. Using the example above,
|
103
|
+
you'd automatically get:
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
Payment.pending # => Mongoid::Criteria
|
107
|
+
Payment.approved # => Mongoid::Criteria
|
108
|
+
Payment.declined # => Mongoid::Criteria
|
109
|
+
```
|
110
|
+
|
111
|
+
|
112
|
+
# Options
|
113
|
+
|
114
|
+
## Default value
|
115
|
+
|
116
|
+
If not specified, the default will be the first in your list of values
|
117
|
+
(`:pending` in the example above). You can override this with the
|
118
|
+
`:default` option:
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
enum :roles, [:manager, :administrator], :default => ""
|
122
|
+
```
|
123
|
+
|
124
|
+
## Multiple values
|
125
|
+
|
126
|
+
Sometimes you'll need to store multiple values from your list, this
|
127
|
+
couldn't be easier:
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
enum :roles, [:basic, :manager, :administrator], :multiple => true
|
131
|
+
|
132
|
+
# ...
|
133
|
+
|
134
|
+
user = User.create
|
135
|
+
user.roles << :basic
|
136
|
+
user.roles << :manager
|
137
|
+
user.save!
|
138
|
+
|
139
|
+
user.manager? # => true
|
140
|
+
user.administrator? # => false
|
141
|
+
user.roles # => [:basic, :manager]
|
142
|
+
```
|
143
|
+
|
144
|
+
Since the underlying datatype for storing values is an array, if you
|
145
|
+
need to specify default(s), ensure you use an array:
|
146
|
+
|
147
|
+
```ruby
|
148
|
+
enum :roles, [:noob, :author, :editor], :multiple => true, :default => [:author, :editor] # two defaults
|
149
|
+
enum :roles, [:noob, :author, :editor], :multiple => true, :default => [] # no default
|
150
|
+
```
|
151
|
+
|
152
|
+
## Validations
|
153
|
+
|
154
|
+
Validations are baked in by default, and ensure that the value(s) set in
|
155
|
+
your field are always from your list of options. If you need more
|
156
|
+
complex validations, or you just want to throw caution to the wind, you
|
157
|
+
can turn them off:
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
enum :status, [:up, :down], :validate => false
|
161
|
+
```
|
162
|
+
|
163
|
+
# Issues and Feature Requests
|
164
|
+
|
165
|
+
If you have any problems, or you have a suggestion, please [submit an
|
166
|
+
issue](https://github.com/thetron/mongoid-enum/issues) (and a failing
|
167
|
+
test, if you can). Pull requests and feature requests are alwasy welcome
|
168
|
+
and greatly appreciated.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/mongoid/enum.rb
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
require "mongoid/enum/version"
|
2
|
+
require "mongoid/enum/validators/multiple_validator"
|
3
|
+
require "mongoid/enum/configuration"
|
4
|
+
|
5
|
+
module Mongoid
|
6
|
+
module Enum
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
def enum(name, values, options = {})
|
11
|
+
field_name = :"#{Mongoid::Enum.configuration.field_name_prefix}#{name}"
|
12
|
+
options = default_options(values).merge(options)
|
13
|
+
|
14
|
+
set_values_constant name, values
|
15
|
+
|
16
|
+
create_field field_name, options
|
17
|
+
|
18
|
+
create_validations field_name, values, options
|
19
|
+
define_value_scopes_and_accessors field_name, values, options
|
20
|
+
define_field_accessor name, field_name, options
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
def default_options(values)
|
25
|
+
{
|
26
|
+
:multiple => false,
|
27
|
+
:default => values.first,
|
28
|
+
:required => true,
|
29
|
+
:validate => true
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
def set_values_constant(name, values)
|
34
|
+
const_name = name.to_s.upcase
|
35
|
+
const_set const_name, values
|
36
|
+
end
|
37
|
+
|
38
|
+
def create_field(field_name, options)
|
39
|
+
type = options[:multiple] && Array || Symbol
|
40
|
+
field field_name, :type => type, :default => options[:default]
|
41
|
+
end
|
42
|
+
|
43
|
+
def create_validations(field_name, values, options)
|
44
|
+
if options[:multiple] && options[:validate]
|
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]` ???
|
47
|
+
elsif validate
|
48
|
+
validates field_name, :inclusion => {:in => values.map(&:to_sym)}, :allow_nil => !options[:required]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def define_value_scopes_and_accessors(field_name, values, options)
|
53
|
+
values.each do |value|
|
54
|
+
scope value, ->{ where(field_name => value) }
|
55
|
+
|
56
|
+
if options[:multiple]
|
57
|
+
define_array_accessor(field_name, value)
|
58
|
+
else
|
59
|
+
define_string_accessor(field_name, value)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
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
|
+
|
82
|
+
def define_array_accessor(field_name, value)
|
83
|
+
class_eval "def #{value}?() self.#{field_name}.include?(:#{value}) end"
|
84
|
+
class_eval "def #{value}!() update_attributes! :#{field_name} => (self.#{field_name} || []) + [:#{value}] end"
|
85
|
+
end
|
86
|
+
|
87
|
+
def define_string_accessor(field_name, value)
|
88
|
+
class_eval "def #{value}?() self.#{field_name} == :#{value} end"
|
89
|
+
class_eval "def #{value}!() update_attributes! :#{field_name} => :#{value} end"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -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
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Enum
|
3
|
+
module Validators
|
4
|
+
class MultipleValidator < ActiveModel::EachValidator
|
5
|
+
def validate_each(record, attribute, values)
|
6
|
+
values = Array(values)
|
7
|
+
|
8
|
+
if options[:allow_nil]
|
9
|
+
add_error_message record, attribute if !all_included?(values, options[:in])
|
10
|
+
else
|
11
|
+
add_error_message record, attribute if values.empty? || !all_included?(values, options[:in])
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def add_error_message(record, attribute)
|
16
|
+
record.errors[attribute] << (options[:message] || "is not in #{options[:in].join ", "}")
|
17
|
+
end
|
18
|
+
|
19
|
+
def all_included?(values, allowed)
|
20
|
+
(values - allowed).empty?
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.kind
|
24
|
+
:custom
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mongoid/enum/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hipaa-mongoid-enum"
|
8
|
+
spec.version = Mongoid::Enum::VERSION
|
9
|
+
spec.authors = ["kennethrosales"]
|
10
|
+
spec.email = ["kenneth@lognllc.com"]
|
11
|
+
|
12
|
+
spec.description = %q{Heavily inspired by DDH's ActiveRecord::Enum, this little library is there to help you cut down the cruft in your models and make the world a happier place at the same time.}
|
13
|
+
spec.summary = %q{Sweet enum sugar for your Mongoid documents}
|
14
|
+
spec.homepage = "https://github.com/kennethrosales/hippa-enum"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
|
18
|
+
spec.files = `git ls-files`.split($/)
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_runtime_dependency "mongoid", ">= 5.0"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
26
|
+
spec.add_development_dependency "rake"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.1"
|
28
|
+
spec.add_development_dependency "guard-rspec", "~> 4.6.2"
|
29
|
+
spec.add_development_dependency "mongoid-rspec", "~> 3.0"
|
30
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
describe Mongoid::Enum::Validators::MultipleValidator do
|
5
|
+
subject { Mongoid::Enum::Validators::MultipleValidator }
|
6
|
+
let(:values) { [:lorem, :ipsum, :dolor, :sit] }
|
7
|
+
let(:attribute) { :word }
|
8
|
+
let(:record) { OpenStruct.new(:errors => {attribute => []}, attribute => values.first) }
|
9
|
+
let(:allow_nil) { false }
|
10
|
+
let(:validator) { subject.new(:attributes => attribute, :in => values, :allow_nil => allow_nil) }
|
11
|
+
|
12
|
+
describe ".validate_each" do
|
13
|
+
context "when allow_nil: true" do
|
14
|
+
let(:allow_nil) { true }
|
15
|
+
|
16
|
+
context "and value is nil" do
|
17
|
+
before(:each) { validator.validate_each(record, attribute, nil) }
|
18
|
+
it "validates" do
|
19
|
+
expect(record.errors[attribute].empty?).to be true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context "and value is []" do
|
24
|
+
before(:each) { validator.validate_each(record, attribute, []) }
|
25
|
+
it "validates" do
|
26
|
+
expect(record.errors[attribute].empty?).to be true
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "when allow_nil: false" do
|
32
|
+
context "and value is nil" do
|
33
|
+
before(:each) { validator.validate_each(record, attribute, nil) }
|
34
|
+
it "won't validate" do
|
35
|
+
expect(record.errors[attribute].any?).to be true
|
36
|
+
expect(record.errors[attribute]).to eq ["is not in #{values.join ", "}"]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
context "and value is []" do
|
40
|
+
before(:each) { validator.validate_each(record, attribute, []) }
|
41
|
+
it "won't validate" do
|
42
|
+
expect(record.errors[attribute].any?).to be true
|
43
|
+
expect(record.errors[attribute]).to eq ["is not in #{values.join ", "}"]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context "when value is included" do
|
49
|
+
let(:allow_nil) { rand(2).zero? }
|
50
|
+
before(:each) { validator.validate_each(record, attribute, [values.sample]) }
|
51
|
+
it "validates" do
|
52
|
+
expect(record.errors[attribute].empty?).to be true
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context "when value is not included" do
|
57
|
+
let(:allow_nil) { rand(2).zero? }
|
58
|
+
before(:each) { validator.validate_each(record, attribute, [:amet]) }
|
59
|
+
it "won't validate" do
|
60
|
+
expect(record.errors[attribute].any?).to be true
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context "when multiple values included" do
|
65
|
+
let(:allow_nil) { rand(2).zero? }
|
66
|
+
before(:each) { validator.validate_each(record, attribute, [values.first, values.last]) }
|
67
|
+
it "validates" do
|
68
|
+
expect(record.errors[attribute].empty?).to be true
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context "when one value is not included "do
|
73
|
+
let(:allow_nil) { rand(2).zero? }
|
74
|
+
before(:each) { validator.validate_each(record, attribute, [values.first, values.last, :amet]) }
|
75
|
+
it "won't validate" do
|
76
|
+
expect(record.errors[attribute].any?).to be true
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,267 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'mongoid/enum/configuration'
|
3
|
+
|
4
|
+
class User
|
5
|
+
include Mongoid::Document
|
6
|
+
include Mongoid::Enum
|
7
|
+
|
8
|
+
enum :status, [:awaiting_approval, :approved, :banned]
|
9
|
+
enum :roles, [:author, :editor, :admin], :multiple => true, :default => [], :required => false
|
10
|
+
end
|
11
|
+
|
12
|
+
describe Mongoid::Enum do
|
13
|
+
let(:klass) { User }
|
14
|
+
let(:instance) { User.new }
|
15
|
+
let(:alias_name) { :status }
|
16
|
+
let(:field_name) { :"_#{alias_name}" }
|
17
|
+
let(:values) { [:awaiting_approval, :approved, :banned] }
|
18
|
+
let(:multiple_field_name) { :"_roles" }
|
19
|
+
|
20
|
+
describe "field" do
|
21
|
+
it "is defined" do
|
22
|
+
expect(klass).to have_field(field_name)
|
23
|
+
end
|
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
|
+
|
42
|
+
it "is aliased" do
|
43
|
+
expect(instance).to respond_to alias_name
|
44
|
+
expect(instance).to respond_to :"#{alias_name}="
|
45
|
+
expect(instance).to respond_to :"#{alias_name}"
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "type" do
|
49
|
+
context "when multiple" do
|
50
|
+
it "is an array" do
|
51
|
+
expect(klass).to have_field(multiple_field_name).of_type(Array)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "validates using a custom validator" do
|
55
|
+
expect(klass).to custom_validate(multiple_field_name).with_validator(Mongoid::Enum::Validators::MultipleValidator)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context "when not multiple" do
|
60
|
+
it "is a symbol" do
|
61
|
+
expect(klass).to have_field(field_name).of_type(Symbol)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "validates inclusion in values" do
|
65
|
+
expect(klass).to validate_inclusion_of(field_name).to_allow(values)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
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
|
+
|
87
|
+
describe "constant" do
|
88
|
+
it "is set to the values" do
|
89
|
+
expect(klass::STATUS).to eq values
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe "accessors" do
|
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
|
+
|
107
|
+
describe "{{value}}!" do
|
108
|
+
it "sets the value" do
|
109
|
+
instance.save
|
110
|
+
instance.banned!
|
111
|
+
expect(instance.status).to eq :banned
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
describe "{{value}}?" do
|
116
|
+
context "when {{enum}} == {{value}}" do
|
117
|
+
it "returns true" do
|
118
|
+
instance.save
|
119
|
+
instance.banned!
|
120
|
+
expect(instance.banned?).to eq true
|
121
|
+
end
|
122
|
+
end
|
123
|
+
context "when {{enum}} != {{value}}" do
|
124
|
+
it "returns false" do
|
125
|
+
instance.save
|
126
|
+
instance.banned!
|
127
|
+
expect(instance.approved?).to eq false
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
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
|
+
|
161
|
+
describe "{{value}}!" do
|
162
|
+
context "when field is nil" do
|
163
|
+
it "creates an array containing the value" do
|
164
|
+
instance.roles = nil
|
165
|
+
instance.save
|
166
|
+
instance.author!
|
167
|
+
expect(instance.roles).to eq [:author]
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
context "when field is not nil" do
|
172
|
+
it "appends the value" do
|
173
|
+
instance.save
|
174
|
+
instance.author!
|
175
|
+
instance.editor!
|
176
|
+
expect(instance.roles).to eq [:author, :editor]
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
describe "{{value}}?" do
|
182
|
+
context "when {{enum}} contains {{value}}" do
|
183
|
+
it "returns true" do
|
184
|
+
instance.save
|
185
|
+
instance.author!
|
186
|
+
instance.editor!
|
187
|
+
expect(instance.editor?).to be true
|
188
|
+
expect(instance.author?).to be true
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
context "when {{enum}} does not contain {{value}}" do
|
193
|
+
it "returns false" do
|
194
|
+
instance.save
|
195
|
+
expect(instance.author?).to be false
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
describe "scopes" do
|
203
|
+
context "when singular" do
|
204
|
+
it "returns the corresponding documents" do
|
205
|
+
instance.save
|
206
|
+
instance.banned!
|
207
|
+
expect(User.banned.to_a).to eq [instance]
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
context "when multiple" do
|
212
|
+
context "and only one document" do
|
213
|
+
it "returns that document" do
|
214
|
+
instance.save
|
215
|
+
instance.author!
|
216
|
+
instance.editor!
|
217
|
+
expect(User.author.to_a).to eq [instance]
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
context "and more than one document" do
|
222
|
+
it "returns all documents with those values" do
|
223
|
+
instance.save
|
224
|
+
instance.author!
|
225
|
+
instance.editor!
|
226
|
+
instance2 = klass.create
|
227
|
+
instance2.author!
|
228
|
+
expect(User.author.to_a).to eq [instance, instance2]
|
229
|
+
expect(User.editor.to_a).to eq [instance]
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
describe "default values" do
|
236
|
+
context "when not specified" do
|
237
|
+
it "uses the first value" do
|
238
|
+
instance.save
|
239
|
+
expect(instance.status).to eq values.first
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
context "when specified" do
|
244
|
+
it "uses the specified value" do
|
245
|
+
instance.save
|
246
|
+
expect(instance.roles).to eq []
|
247
|
+
end
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
describe ".configuration" do
|
252
|
+
it "returns Configuration object" do
|
253
|
+
expect(Mongoid::Enum.configuration)
|
254
|
+
.to be_instance_of Mongoid::Enum::Configuration
|
255
|
+
end
|
256
|
+
it "returns same object when called multiple times" do
|
257
|
+
expect(Mongoid::Enum.configuration).to be Mongoid::Enum.configuration
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
describe ".configure" do
|
262
|
+
it "yields configuration if block is given" do
|
263
|
+
expect { |b| Mongoid::Enum.configure &b }
|
264
|
+
.to yield_with_args Mongoid::Enum.configuration
|
265
|
+
end
|
266
|
+
end
|
267
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
$: << File.expand_path("../../lib", __FILE__)
|
2
|
+
|
3
|
+
require 'mongoid'
|
4
|
+
require "mongoid/rspec"
|
5
|
+
require 'mongoid/enum'
|
6
|
+
|
7
|
+
ENV['MONGOID_ENV'] = "test"
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
config.include Mongoid::Matchers
|
11
|
+
|
12
|
+
config.before(:each) do
|
13
|
+
Mongoid.purge!
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
Mongoid.load!(File.expand_path("../support/mongoid.yml", __FILE__), :test)
|
18
|
+
Mongo::Logger.logger.level = ::Logger::INFO
|
metadata
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hipaa-mongoid-enum
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- kennethrosales
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-10-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mongoid
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.1'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.1'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: guard-rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 4.6.2
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 4.6.2
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: mongoid-rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
description: Heavily inspired by DDH's ActiveRecord::Enum, this little library is
|
98
|
+
there to help you cut down the cruft in your models and make the world a happier
|
99
|
+
place at the same time.
|
100
|
+
email:
|
101
|
+
- kenneth@lognllc.com
|
102
|
+
executables: []
|
103
|
+
extensions: []
|
104
|
+
extra_rdoc_files: []
|
105
|
+
files:
|
106
|
+
- ".autotest"
|
107
|
+
- ".gitignore"
|
108
|
+
- ".rspec"
|
109
|
+
- ".travis.yml"
|
110
|
+
- Gemfile
|
111
|
+
- Guardfile
|
112
|
+
- LICENSE.txt
|
113
|
+
- README.md
|
114
|
+
- Rakefile
|
115
|
+
- lib/mongoid/enum.rb
|
116
|
+
- lib/mongoid/enum/configuration.rb
|
117
|
+
- lib/mongoid/enum/validators/multiple_validator.rb
|
118
|
+
- lib/mongoid/enum/version.rb
|
119
|
+
- mongoid-enum.gemspec
|
120
|
+
- spec/mongoid/configuration_spec.rb
|
121
|
+
- spec/mongoid/enum/validators/multiple_validator_spec.rb
|
122
|
+
- spec/mongoid/enum_spec.rb
|
123
|
+
- spec/spec_helper.rb
|
124
|
+
- spec/support/mongoid.yml
|
125
|
+
homepage: https://github.com/kennethrosales/hippa-enum
|
126
|
+
licenses:
|
127
|
+
- MIT
|
128
|
+
metadata: {}
|
129
|
+
post_install_message:
|
130
|
+
rdoc_options: []
|
131
|
+
require_paths:
|
132
|
+
- lib
|
133
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
139
|
+
requirements:
|
140
|
+
- - ">="
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '0'
|
143
|
+
requirements: []
|
144
|
+
rubyforge_project:
|
145
|
+
rubygems_version: 2.5.2
|
146
|
+
signing_key:
|
147
|
+
specification_version: 4
|
148
|
+
summary: Sweet enum sugar for your Mongoid documents
|
149
|
+
test_files:
|
150
|
+
- spec/mongoid/configuration_spec.rb
|
151
|
+
- spec/mongoid/enum/validators/multiple_validator_spec.rb
|
152
|
+
- spec/mongoid/enum_spec.rb
|
153
|
+
- spec/spec_helper.rb
|
154
|
+
- spec/support/mongoid.yml
|