diff --git a/Gemfile b/Gemfile old mode 100644 new mode 100755 diff --git a/Guardfile b/Guardfile old mode 100644 new mode 100755 index ee91a50..b0f555b --- a/Guardfile +++ b/Guardfile @@ -1,8 +1,10 @@ +# frozen_string_literal: true + # A sample Guardfile # More info at https://github.com/guard/guard#readme -guard :rspec, cmd: "bundle exec rspec" do +guard :rspec, cmd: 'bundle exec rspec' do watch(%r{^spec/.+_spec\.rb$}) watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } - watch('spec/spec_helper.rb') { "spec" } + watch('spec/spec_helper.rb') { 'spec' } end diff --git a/LICENSE.txt b/LICENSE.txt old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/Rakefile b/Rakefile old mode 100644 new mode 100755 index 2995527..c702cfc --- a/Rakefile +++ b/Rakefile @@ -1 +1 @@ -require "bundler/gem_tasks" +require 'bundler/gem_tasks' diff --git a/lib/mongoid/enum.rb b/lib/mongoid/enum.rb old mode 100644 new mode 100755 index 20dc37e..f065606 --- a/lib/mongoid/enum.rb +++ b/lib/mongoid/enum.rb @@ -1,12 +1,13 @@ -require "mongoid/enum/version" -require "mongoid/enum/validators/multiple_validator" -require "mongoid/enum/configuration" +# frozen_string_literal: true + +require 'mongoid/enum/version' +require 'mongoid/enum/validators/multiple_validator' +require 'mongoid/enum/configuration' module Mongoid module Enum extend ActiveSupport::Concern module ClassMethods - def enum(name, values, options = {}) field_name = :"#{Mongoid::Enum.configuration.field_name_prefix}#{name}" options = default_options(values).merge(options) @@ -21,12 +22,13 @@ def enum(name, values, options = {}) end private + def default_options(values) { - :multiple => false, - :default => values.first, - :required => true, - :validate => true + multiple: false, + default: values.first, + required: true, + validate: true } end @@ -37,21 +39,21 @@ def set_values_constant(name, values) def create_field(field_name, options) type = options[:multiple] && Array || Symbol - field field_name, :type => type, :default => options[:default] + field field_name, type: type, default: options[:default] end def create_validations(field_name, values, options) if options[:multiple] && options[:validate] - validates field_name, :'mongoid/enum/validators/multiple' => { :in => values.map(&:to_sym), :allow_nil => !options[:required] } - #FIXME: Shouldn't this be `elsif options[:validate]` ??? + validates field_name, 'mongoid/enum/validators/multiple': { in: values.map(&:to_sym), allow_nil: !options[:required] } + # FIXME: Shouldn't this be `elsif options[:validate]` ??? elsif validate - validates field_name, :inclusion => {:in => values.map(&:to_sym)}, :allow_nil => !options[:required] + validates field_name, inclusion: { in: values.map(&:to_sym) }, allow_nil: !options[:required] end end def define_value_scopes_and_accessors(field_name, values, options) values.each do |value| - scope value, ->{ where(field_name => value) } + scope value, -> { where(field_name => value) } if options[:multiple] define_array_accessor(field_name, value) diff --git a/lib/mongoid/enum/configuration.rb b/lib/mongoid/enum/configuration.rb old mode 100644 new mode 100755 index ed45df4..8879898 --- a/lib/mongoid/enum/configuration.rb +++ b/lib/mongoid/enum/configuration.rb @@ -1,10 +1,12 @@ +# frozen_string_literal: true + module Mongoid module Enum class Configuration attr_accessor :field_name_prefix def initialize - self.field_name_prefix = "_" + self.field_name_prefix = '_' end end diff --git a/lib/mongoid/enum/validators/multiple_validator.rb b/lib/mongoid/enum/validators/multiple_validator.rb old mode 100644 new mode 100755 index 3360816..1e7f5e2 --- a/lib/mongoid/enum/validators/multiple_validator.rb +++ b/lib/mongoid/enum/validators/multiple_validator.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Mongoid module Enum module Validators @@ -6,14 +8,14 @@ def validate_each(record, attribute, values) values = Array(values) if options[:allow_nil] - add_error_message record, attribute if !all_included?(values, options[:in]) + add_error_message record, attribute unless all_included?(values, options[:in]) else add_error_message record, attribute if values.empty? || !all_included?(values, options[:in]) end end def add_error_message(record, attribute) - record.errors[attribute] << (options[:message] || "is not in #{options[:in].join ", "}") + record.errors[attribute] << (options[:message] || "is not in #{options[:in].join ', '}") end def all_included?(values, allowed) diff --git a/lib/mongoid/enum/version.rb b/lib/mongoid/enum/version.rb old mode 100644 new mode 100755 index 0067529..6f672e9 --- a/lib/mongoid/enum/version.rb +++ b/lib/mongoid/enum/version.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + module Mongoid module Enum - VERSION = "0.4.0" + VERSION = '0.4.0' end end diff --git a/mongoid-enum.gemspec b/mongoid-enum.gemspec old mode 100644 new mode 100755 index bd9465d..a3f962d --- a/mongoid-enum.gemspec +++ b/mongoid-enum.gemspec @@ -1,28 +1,29 @@ -# coding: utf-8 -lib = File.expand_path('../lib', __FILE__) +# frozen_string_literal: true + +lib = File.expand_path('lib', __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'mongoid/enum/version' Gem::Specification.new do |spec| - spec.name = "mongoid-enum" + spec.name = 'mongoid-enum' spec.version = Mongoid::Enum::VERSION - spec.authors = ["Nicholas Bruning"] - spec.email = ["nicholas@bruning.com.au"] - 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.} - spec.summary = %q{Sweet enum sugar for your Mongoid documents} - spec.homepage = "https://github.com/thetron/mongoid-enum" - spec.license = "MIT" + spec.authors = ['Nicholas Bruning'] + spec.email = ['nicholas@bruning.com.au'] + 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." + spec.summary = 'Sweet enum sugar for your Mongoid documents' + spec.homepage = 'https://github.com/thetron/mongoid-enum' + spec.license = 'MIT' - spec.files = `git ls-files`.split($/) + spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) - spec.require_paths = ["lib"] + spec.require_paths = ['lib'] - spec.add_runtime_dependency "mongoid", "~> 5.0" + spec.add_runtime_dependency 'mongoid', '>= 5.0' - spec.add_development_dependency "bundler", "~> 1.3" - spec.add_development_dependency "rake" - spec.add_development_dependency "rspec", "~> 3.1" - spec.add_development_dependency "guard-rspec", "~> 4.6.2" - spec.add_development_dependency "mongoid-rspec", "~> 3.0" + spec.add_development_dependency 'bundler', '~> 1.3' + spec.add_development_dependency 'rake' + spec.add_development_dependency 'rspec', '~> 3.1' + spec.add_development_dependency 'guard-rspec', '~> 4.6.2' + spec.add_development_dependency 'mongoid-rspec', '~> 3.0' end diff --git a/spec/mongoid/configuration_spec.rb b/spec/mongoid/configuration_spec.rb old mode 100644 new mode 100755 index db04d68..f903fa6 --- a/spec/mongoid/configuration_spec.rb +++ b/spec/mongoid/configuration_spec.rb @@ -1,11 +1,13 @@ +# frozen_string_literal: true + require 'spec_helper' describe Mongoid::Enum::Configuration do subject { Mongoid::Enum::Configuration.new } - describe "field_name_prefix" do + describe 'field_name_prefix' do it "has '_' as default value" do - expect(subject.field_name_prefix).to eq "_" + expect(subject.field_name_prefix).to eq '_' end end end diff --git a/spec/mongoid/enum/validators/multiple_validator_spec.rb b/spec/mongoid/enum/validators/multiple_validator_spec.rb old mode 100644 new mode 100755 index d47c605..613a7a4 --- a/spec/mongoid/enum/validators/multiple_validator_spec.rb +++ b/spec/mongoid/enum/validators/multiple_validator_spec.rb @@ -1,59 +1,61 @@ +# frozen_string_literal: true + require 'spec_helper' require 'ostruct' describe Mongoid::Enum::Validators::MultipleValidator do subject { Mongoid::Enum::Validators::MultipleValidator } - let(:values) { [:lorem, :ipsum, :dolor, :sit] } + let(:values) { %i[lorem ipsum dolor sit] } let(:attribute) { :word } - let(:record) { OpenStruct.new(:errors => {attribute => []}, attribute => values.first) } + let(:record) { OpenStruct.new(:errors => { attribute => [] }, attribute => values.first) } let(:allow_nil) { false } - let(:validator) { subject.new(:attributes => attribute, :in => values, :allow_nil => allow_nil) } + let(:validator) { subject.new(attributes: attribute, in: values, allow_nil: allow_nil) } - describe ".validate_each" do - context "when allow_nil: true" do + describe '.validate_each' do + context 'when allow_nil: true' do let(:allow_nil) { true } - context "and value is nil" do + context 'and value is nil' do before(:each) { validator.validate_each(record, attribute, nil) } - it "validates" do + it 'validates' do expect(record.errors[attribute].empty?).to be true end end - context "and value is []" do + context 'and value is []' do before(:each) { validator.validate_each(record, attribute, []) } - it "validates" do + it 'validates' do expect(record.errors[attribute].empty?).to be true end end end - context "when allow_nil: false" do - context "and value is nil" do + context 'when allow_nil: false' do + context 'and value is nil' do before(:each) { validator.validate_each(record, attribute, nil) } it "won't validate" do expect(record.errors[attribute].any?).to be true - expect(record.errors[attribute]).to eq ["is not in #{values.join ", "}"] + expect(record.errors[attribute]).to eq ["is not in #{values.join ', '}"] end end - context "and value is []" do + context 'and value is []' do before(:each) { validator.validate_each(record, attribute, []) } it "won't validate" do expect(record.errors[attribute].any?).to be true - expect(record.errors[attribute]).to eq ["is not in #{values.join ", "}"] + expect(record.errors[attribute]).to eq ["is not in #{values.join ', '}"] end end end - context "when value is included" do + context 'when value is included' do let(:allow_nil) { rand(2).zero? } before(:each) { validator.validate_each(record, attribute, [values.sample]) } - it "validates" do + it 'validates' do expect(record.errors[attribute].empty?).to be true end end - context "when value is not included" do + context 'when value is not included' do let(:allow_nil) { rand(2).zero? } before(:each) { validator.validate_each(record, attribute, [:amet]) } it "won't validate" do @@ -61,15 +63,15 @@ end end - context "when multiple values included" do + context 'when multiple values included' do let(:allow_nil) { rand(2).zero? } before(:each) { validator.validate_each(record, attribute, [values.first, values.last]) } - it "validates" do + it 'validates' do expect(record.errors[attribute].empty?).to be true end end - context "when one value is not included "do + context 'when one value is not included ' do let(:allow_nil) { rand(2).zero? } before(:each) { validator.validate_each(record, attribute, [values.first, values.last, :amet]) } it "won't validate" do diff --git a/spec/mongoid/enum_spec.rb b/spec/mongoid/enum_spec.rb old mode 100644 new mode 100755 index 5bd9c3c..6003e42 --- a/spec/mongoid/enum_spec.rb +++ b/spec/mongoid/enum_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' require 'mongoid/enum/configuration' @@ -5,8 +7,8 @@ class User include Mongoid::Document include Mongoid::Enum - enum :status, [:awaiting_approval, :approved, :banned] - enum :roles, [:author, :editor, :admin], :multiple => true, :default => [], :required => false + enum :status, %i[awaiting_approval approved banned] + enum :roles, %i[author editor admin], multiple: true, default: [], required: false end describe Mongoid::Enum do @@ -14,54 +16,54 @@ class User let(:instance) { User.new } let(:alias_name) { :status } let(:field_name) { :"_#{alias_name}" } - let(:values) { [:awaiting_approval, :approved, :banned] } - let(:multiple_field_name) { :"_roles" } + let(:values) { %i[awaiting_approval approved banned] } + let(:multiple_field_name) { :_roles } - describe "field" do - it "is defined" do + describe 'field' do + it 'is defined' do expect(klass).to have_field(field_name) end - it "uses prefix defined in configuration" do + it 'uses prefix defined in configuration' do old_field_name_prefix = Mongoid::Enum.configuration.field_name_prefix Mongoid::Enum.configure do |config| - config.field_name_prefix = "___" + config.field_name_prefix = '___' end UserWithoutPrefix = Class.new do include Mongoid::Document include Mongoid::Enum - enum :status, [:awaiting_approval, :approved, :banned] + enum :status, %i[awaiting_approvalapproved banned] end - expect(UserWithoutPrefix).to have_field "___status" + expect(UserWithoutPrefix).to have_field '___status' Mongoid::Enum.configure do |config| config.field_name_prefix = old_field_name_prefix end end - it "is aliased" do + it 'is aliased' do expect(instance).to respond_to alias_name expect(instance).to respond_to :"#{alias_name}=" expect(instance).to respond_to :"#{alias_name}" end - describe "type" do - context "when multiple" do - it "is an array" do + describe 'type' do + context 'when multiple' do + it 'is an array' do expect(klass).to have_field(multiple_field_name).of_type(Array) end - it "validates using a custom validator" do + it 'validates using a custom validator' do expect(klass).to custom_validate(multiple_field_name).with_validator(Mongoid::Enum::Validators::MultipleValidator) end end - context "when not multiple" do - it "is a symbol" do + context 'when not multiple' do + it 'is a symbol' do expect(klass).to have_field(field_name).of_type(Symbol) end - it "validates inclusion in values" do + it 'validates inclusion in values' do expect(klass).to validate_inclusion_of(field_name).to_allow(values) end end @@ -69,59 +71,59 @@ class User end describe "'required' option" do - context "when true" do + context 'when true' do let(:instance) { User.new status: nil } - it "is not valid with nil value" do + it 'is not valid with nil value' do expect(instance).to_not be_valid end end - context "when false" do + context 'when false' do let(:instance) { User.new roles: nil } - it "is valid with nil value" do + it 'is valid with nil value' do expect(instance).to be_valid end end end - describe "constant" do - it "is set to the values" do + describe 'constant' do + it 'is set to the values' do expect(klass::STATUS).to eq values end end - describe "accessors" do - context "when singular" do - describe "setter" do - it "accepts strings" do + describe 'accessors' do + context 'when singular' do + describe 'setter' do + it 'accepts strings' do instance.status = 'banned' expect(instance.status).to eq :banned end - it "accepts symbols" do + it 'accepts symbols' do instance.status = :banned expect(instance.status).to eq :banned end end - describe "{{value}}!" do - it "sets the value" do + describe '{{value}}!' do + it 'sets the value' do instance.save instance.banned! expect(instance.status).to eq :banned end end - describe "{{value}}?" do - context "when {{enum}} == {{value}}" do - it "returns true" do + describe '{{value}}?' do + context 'when {{enum}} == {{value}}' do + it 'returns true' do instance.save instance.banned! expect(instance.banned?).to eq true end end - context "when {{enum}} != {{value}}" do - it "returns false" do + context 'when {{enum}} != {{value}}' do + it 'returns false' do instance.save instance.banned! expect(instance.approved?).to eq false @@ -130,19 +132,19 @@ class User end end - context "when multiple" do - describe "setter" do - it "accepts strings" do - instance.roles = "author" + context 'when multiple' do + describe 'setter' do + it 'accepts strings' do + instance.roles = 'author' expect(instance.roles).to eq [:author] end - it "accepts symbols" do + it 'accepts symbols' do instance.roles = :author expect(instance.roles).to eq [:author] end - it "accepts arrays of strings" do + it 'accepts arrays of strings' do instance.roles = ['author', 'editor'] instance.save puts instance.errors.full_messages @@ -151,16 +153,16 @@ class User expect(instance.roles).to include(:editor) end - it "accepts arrays of symbols" do + it 'accepts arrays of symbols' do instance.roles = [:author, :editor] expect(instance.roles).to include(:author) expect(instance.roles).to include(:editor) end end - describe "{{value}}!" do - context "when field is nil" do - it "creates an array containing the value" do + describe '{{value}}!' do + context 'when field is nil' do + it 'creates an array containing the value' do instance.roles = nil instance.save instance.author! @@ -168,19 +170,19 @@ class User end end - context "when field is not nil" do - it "appends the value" do + context 'when field is not nil' do + it 'appends the value' do instance.save instance.author! instance.editor! - expect(instance.roles).to eq [:author, :editor] + expect(instance.roles).to eq %i[author editor] end end end - describe "{{value}}?" do - context "when {{enum}} contains {{value}}" do - it "returns true" do + describe '{{value}}?' do + context 'when {{enum}} contains {{value}}' do + it 'returns true' do instance.save instance.author! instance.editor! @@ -189,8 +191,8 @@ class User end end - context "when {{enum}} does not contain {{value}}" do - it "returns false" do + context 'when {{enum}} does not contain {{value}}' do + it 'returns false' do instance.save expect(instance.author?).to be false end @@ -199,18 +201,18 @@ class User end end - describe "scopes" do - context "when singular" do - it "returns the corresponding documents" do + describe 'scopes' do + context 'when singular' do + it 'returns the corresponding documents' do instance.save instance.banned! expect(User.banned.to_a).to eq [instance] end end - context "when multiple" do - context "and only one document" do - it "returns that document" do + context 'when multiple' do + context 'and only one document' do + it 'returns that document' do instance.save instance.author! instance.editor! @@ -218,8 +220,8 @@ class User end end - context "and more than one document" do - it "returns all documents with those values" do + context 'and more than one document' do + it 'returns all documents with those values' do instance.save instance.author! instance.editor! @@ -232,36 +234,36 @@ class User end end - describe "default values" do - context "when not specified" do - it "uses the first value" do + describe 'default values' do + context 'when not specified' do + it 'uses the first value' do instance.save expect(instance.status).to eq values.first end end - context "when specified" do - it "uses the specified value" do + context 'when specified' do + it 'uses the specified value' do instance.save expect(instance.roles).to eq [] end end end - describe ".configuration" do - it "returns Configuration object" do + describe '.configuration' do + it 'returns Configuration object' do expect(Mongoid::Enum.configuration) - .to be_instance_of Mongoid::Enum::Configuration + .to be_instance_of Mongoid::Enum::Configuration end - it "returns same object when called multiple times" do + it 'returns same object when called multiple times' do expect(Mongoid::Enum.configuration).to be Mongoid::Enum.configuration end end - describe ".configure" do - it "yields configuration if block is given" do + describe '.configure' do + it 'yields configuration if block is given' do expect { |b| Mongoid::Enum.configure &b } - .to yield_with_args Mongoid::Enum.configuration + .to yield_with_args Mongoid::Enum.configuration end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb old mode 100644 new mode 100755 diff --git a/spec/support/mongoid.yml b/spec/support/mongoid.yml old mode 100644 new mode 100755