mongoid-enum-logn 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a4d73a17bdea83319aa258345960004a399c2840
4
+ data.tar.gz: 06b72e7b2191fec9b0eb6f3cd6c9977e2fe4f7b4
5
+ SHA512:
6
+ metadata.gz: 7a6a50b4c4b46ae5fc4ad36de844fb572134a081cf520feb8b2f90bdf9fade5ce9bdff4ae02703296da47ce7f5cc77c89cb3513536eac0322a95476551fd1af3
7
+ data.tar.gz: c4da5ecb6f255ff54bbdac3aecb0c7c9f6660d49c25e9d7245e32e3ca286a0720e2c9ddc653841e02d528c76ee8f79901d43de54526e1617959f5b62d12cfd19
data/.autotest ADDED
File without changes
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at kenneth@lognllc.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongoid-enum.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,8 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec, cmd: "bundle exec rspec" do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
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
+ [![Build
4
+ Status](https://travis-ci.org/thetron/mongoid-enum.png)](https://travis-ci.org/thetron/mongoid-enum)
5
+ [![Code Climate](https://codeclimate.com/github/thetron/mongoid-enum.png)](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/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mongoid/enum/logn"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -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,5 @@
1
+ module Mongoid
2
+ module Enum
3
+ VERSION = '0.0.1'.freeze
4
+ end
5
+ end
@@ -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,28 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'mongoid/enum/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "mongoid-enum-logn"
7
+ spec.version = Mongoid::Enum::VERSION
8
+ spec.authors = ['kennethrosales']
9
+ spec.email = ['kenneth@lognllc.com']
10
+
11
+ spec.description = "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."
12
+ spec.summary = 'Sweet enum sugar for your Mongoid documents'
13
+ spec.homepage = 'https://github.com/thetron/mongoid-enum'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_runtime_dependency 'mongoid', '>= 5.0'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.3'
24
+ spec.add_development_dependency 'guard-rspec', '~> 4.6.2'
25
+ spec.add_development_dependency 'mongoid-rspec', '~> 3.0'
26
+ spec.add_development_dependency 'rake'
27
+ spec.add_development_dependency 'rspec', '~> 3.1'
28
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Enum::Configuration do
4
+ subject { Mongoid::Enum::Configuration.new }
5
+
6
+ describe "field_name_prefix" do
7
+ it "has '_' as default value" do
8
+ expect(subject.field_name_prefix).to eq "_"
9
+ end
10
+ end
11
+ 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
@@ -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
@@ -0,0 +1,6 @@
1
+ test:
2
+ clients:
3
+ default:
4
+ database: mongoid-enum_test
5
+ hosts:
6
+ - localhost:<%= ENV['BOXEN_MONGODB_PORT'] || 27017 %>
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-enum-logn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - kennethrosales
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-10-17 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: guard-rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 4.6.2
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 4.6.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: mongoid-rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.1'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.1'
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
+ - console
104
+ - setup
105
+ extensions: []
106
+ extra_rdoc_files: []
107
+ files:
108
+ - ".autotest"
109
+ - ".gitignore"
110
+ - CODE_OF_CONDUCT.md
111
+ - Gemfile
112
+ - Guardfile
113
+ - LICENSE.txt
114
+ - README.md
115
+ - Rakefile
116
+ - bin/console
117
+ - bin/setup
118
+ - lib/mongoid/enum.rb
119
+ - lib/mongoid/enum/configuration.rb
120
+ - lib/mongoid/enum/validators/multiple_validator.rb
121
+ - lib/mongoid/enum/version.rb
122
+ - mongoid-enum-logn.gemspec
123
+ - spec/mongoid/configuration_spec.rb
124
+ - spec/mongoid/enum/validators/multiple_validator_spec.rb
125
+ - spec/mongoid/enum_spec.rb
126
+ - spec/spec_helper.rb
127
+ - spec/support/mongoid.yml
128
+ homepage: https://github.com/thetron/mongoid-enum
129
+ licenses:
130
+ - MIT
131
+ metadata: {}
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubyforge_project:
148
+ rubygems_version: 2.5.2
149
+ signing_key:
150
+ specification_version: 4
151
+ summary: Sweet enum sugar for your Mongoid documents
152
+ test_files:
153
+ - spec/mongoid/configuration_spec.rb
154
+ - spec/mongoid/enum/validators/multiple_validator_spec.rb
155
+ - spec/mongoid/enum_spec.rb
156
+ - spec/spec_helper.rb
157
+ - spec/support/mongoid.yml