diff --git a/Gemfile b/Gemfile index 922ef96..13a9d45 100644 --- a/Gemfile +++ b/Gemfile @@ -8,11 +8,11 @@ gem 'mongoid', '>= 2.0' # Include everything needed to run rake, tests, features, etc. group :development do gem 'bundler' + gem 'database_cleaner', '>= 0.8.0' gem 'jeweler', '~> 2.1.1' - gem 'rake' gem 'rack', '~> 1.6.4' + gem 'rake' gem 'rdoc' gem 'rspec', '>= 2.0.0' gem 'simplecov', '>= 0.4.0', require: false - gem 'database_cleaner', '>= 0.8.0' end diff --git a/Rakefile b/Rakefile index 807e40b..836f2d8 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,3 @@ -# encoding: utf-8 - require 'rubygems' require 'rake' require 'bundler' @@ -8,21 +6,21 @@ Bundler::GemHelper.install_tasks begin Bundler.setup(:default, :development) rescue Bundler::BundlerError => e - $stderr.puts e.message - $stderr.puts "Run `bundle install` to install missing gems" + warn e.message + warn 'Run `bundle install` to install missing gems' exit e.status_code end require 'jeweler' Jeweler::Tasks.new do |gem| # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options - gem.name = "mongoid_auto_increment" - gem.homepage = "http://github.com/proton/mongoid_auto_increment" - gem.license = "MIT" - gem.summary = %q{Auto-incrementing fields for Mongoid documents} - gem.description = %q{Add SQL like auto-incrementing fields to your Mongoid documents.} - gem.email = "psavichev@gmail.com" - gem.authors = ["Peter Savichev (proton)"] + gem.name = 'mongoid_auto_increment' + gem.homepage = 'http://github.com/proton/mongoid_auto_increment' + gem.license = 'MIT' + gem.summary = 'Auto-incrementing fields for Mongoid documents' + gem.description = 'Add SQL like auto-incrementing fields to your Mongoid documents.' + gem.email = 'psavichev@gmail.com' + gem.authors = ['Peter Savichev (proton)'] # dependencies defined in Gemfile end Jeweler::RubygemsDotOrgTasks.new @@ -33,11 +31,11 @@ RSpec::Core::RakeTask.new(:spec) do |spec| spec.pattern = 'spec/**/*_spec.rb' end -task :default => :spec +task default: :spec require 'rdoc/task' RDoc::Task.new do |rdoc| - version = File.exist?('VERSION') ? File.read('VERSION') : "" + version = File.exist?('VERSION') ? File.read('VERSION') : '' rdoc.rdoc_dir = 'rdoc' rdoc.title = "mongoid_auto_increment #{version}" @@ -45,15 +43,15 @@ RDoc::Task.new do |rdoc| rdoc.rdoc_files.include('lib/**/*.rb') end -desc "Build gem" +desc 'Build gem' task :build do - puts "Regenerating gemspec" - system "rake gemspec" - puts "Building" - system "gem build mongoid_auto_increment.gemspec" + puts 'Regenerating gemspec' + system 'rake gemspec' + puts 'Building' + system 'gem build mongoid_auto_increment.gemspec' end -desc "Release gem" -task :release => :build do - version = File.exist?('VERSION') ? File.read('VERSION') : "" +desc 'Release gem' +task release: :build do + version = File.exist?('VERSION') ? File.read('VERSION') : '' end diff --git a/lib/mongoid_auto_increment.rb b/lib/mongoid_auto_increment.rb index 3a5684c..848356b 100644 --- a/lib/mongoid_auto_increment.rb +++ b/lib/mongoid_auto_increment.rb @@ -4,8 +4,8 @@ module MongoidAutoIncrement extend ActiveSupport::Concern module ClassMethods - def auto_increment(name, options={}) - field name, :type => Integer + def auto_increment(name, options = {}) + field name, type: Integer unless defined? @@incrementor @@incrementor = MongoidAutoIncrement::Incrementor.new diff --git a/lib/mongoid_auto_increment/incrementor.rb b/lib/mongoid_auto_increment/incrementor.rb index c37f3c9..4554ee0 100644 --- a/lib/mongoid_auto_increment/incrementor.rb +++ b/lib/mongoid_auto_increment/incrementor.rb @@ -1,5 +1,5 @@ # This is a modified version of the code found on this blog post: -# http://ihswebdesign.com/blog/autoincrement-in-mongodb-with-ruby/ +# http://ihswebdesign.com/blog/autoincrement-in-mongodb-with-ruby/ module MongoidAutoIncrement class Incrementor class Sequence @@ -11,26 +11,26 @@ def initialize(sequence, collection_name, seed, step, scope) @step = step.to_i end - def inc + def inc if defined?(::Mongoid::VERSION) && ::Mongoid::VERSION >= '5' collection.find(query).find_one_and_update({ '$inc' => { number: @step } }, new: true, upsert: true, return_document: :after)['number'] elsif defined?(::Mongoid::VERSION) && ::Mongoid::VERSION >= '3' collection.find(query).modify({ '$inc' => { number: @step } }, new: true, upsert: true)['number'] else opts = { - "query" => query, - "update" => {"$inc" => { "number" => @step }}, - "new" => true # return the modified document + 'query' => query, + 'update' => { '$inc' => { 'number' => @step } }, + 'new' => true # return the modified document } - collection.find_and_modify(opts)["number"] + collection.find_and_modify(opts)['number'] end - end + end def current if defined?(::Mongoid::VERSION) && ::Mongoid::VERSION >= '3' - collection.find(query).one["number"] + collection.find(query).one['number'] else - collection.find_one(query)["number"] + collection.find_one(query)['number'] end end @@ -42,9 +42,9 @@ def exists? def create(number) if ::Mongoid::VERSION >= '5' - collection.insert_one(query.merge({ "number" => number })) + collection.insert_one(query.merge('number' => number)) else - collection.insert(query.merge({ "number" => number })) + collection.insert(query.merge('number' => number)) end end @@ -59,15 +59,14 @@ def collection end def query - @scope.merge("seq_name" => @sequence) + @scope.merge('seq_name' => @sequence) end end - def initialize(options=nil) - end + def initialize(options = nil); end def inc(sequence, options, record) - collection = options[:collection] || "sequences" + collection = options[:collection] || 'sequences' seed = options[:seed].to_i step = options[:step] || 1 scope = resolve_scope(record, options[:scope]) diff --git a/spec/models/invoice.rb b/spec/models/invoice.rb index 6e371a0..66a8f9a 100644 --- a/spec/models/invoice.rb +++ b/spec/models/invoice.rb @@ -4,6 +4,5 @@ class Invoice field :name - auto_increment :num, :seed => 1000, :step => 5 + auto_increment :num, seed: 1000, step: 5 end - diff --git a/spec/models/order.rb b/spec/models/order.rb index 78f007a..a289d46 100644 --- a/spec/models/order.rb +++ b/spec/models/order.rb @@ -4,5 +4,5 @@ class Order field :title - auto_increment :num, :seed => 1000 + auto_increment :num, seed: 1000 end diff --git a/spec/models/post.rb b/spec/models/post.rb index fdcb6c6..c92b9ca 100644 --- a/spec/models/post.rb +++ b/spec/models/post.rb @@ -4,7 +4,7 @@ class Post field :title - auto_increment :key, :seed => 500 + auto_increment :key, seed: 500 auto_increment :num embeds_many :comments diff --git a/spec/mongoid_auto_increment_spec.rb b/spec/mongoid_auto_increment_spec.rb index 85b4cc3..8bb0ca7 100644 --- a/spec/mongoid_auto_increment_spec.rb +++ b/spec/mongoid_auto_increment_spec.rb @@ -1,25 +1,24 @@ require 'spec_helper' describe 'mongoid_auto_increment' do - before(:each) do - @book1 = Book.create :name => 'The Rails Way' - @book2 = Book.create :name => 'The Ruby Programming Language ' - @book3 = Book.create :name => 'Metaprogramming Ruby' - @order1 = Order.create :name => 'First Order' - @order2 = Order.create :name => 'Second Order' - @order3 = Order.create :name => 'Last Order' - @post1 = Post.create :name => 'First Post' - @post2 = Post.create :name => 'Second Post' - @post3 = Post.create :name => 'Last Post' - @comment1 = @post1.comments.create :name => 'First Comment' - @comment2 = @post1.comments.create :name => 'Second Comment' - @invoice1 = Invoice.create :name => 'First invoice' - @invoice2 = Invoice.create :name => 'Second invoice' - @invoice3 = Invoice.create :name => 'Third invoice' - @record1 = Record.create :name => 'First record' - @record2 = Record.create :name => 'Second record' - @record3 = Record.create :name => 'Third record' + @book1 = Book.create name: 'The Rails Way' + @book2 = Book.create name: 'The Ruby Programming Language ' + @book3 = Book.create name: 'Metaprogramming Ruby' + @order1 = Order.create name: 'First Order' + @order2 = Order.create name: 'Second Order' + @order3 = Order.create name: 'Last Order' + @post1 = Post.create name: 'First Post' + @post2 = Post.create name: 'Second Post' + @post3 = Post.create name: 'Last Post' + @comment1 = @post1.comments.create name: 'First Comment' + @comment2 = @post1.comments.create name: 'Second Comment' + @invoice1 = Invoice.create name: 'First invoice' + @invoice2 = Invoice.create name: 'Second invoice' + @invoice3 = Invoice.create name: 'Third invoice' + @record1 = Record.create name: 'First record' + @record2 = Record.create name: 'Second record' + @record3 = Record.create name: 'Third record' end describe 'single auto-increment field' do @@ -120,7 +119,7 @@ def collection end it 'should have a sequence name of "Record"' do - expect(collection.find(:seq_name => 'Record').first).not_to eq nil + expect(collection.find(seq_name: 'Record').first).not_to eq nil end end @@ -137,8 +136,8 @@ def collection end it 'should increment each scope separately' do - 3.times.each{ |n| @recipe1.ingredients.create(name: n + 1) } - 9.times.each{ |n| @recipe2.ingredients.create(name: n + 1) } + 3.times.each { |n| @recipe1.ingredients.create(name: n + 1) } + 9.times.each { |n| @recipe2.ingredients.create(name: n + 1) } expect(Ingredient.find_by(recipe: @recipe2, name: 3).order).to eq 3 expect(Ingredient.find_by(recipe: @recipe2, name: 9).order).to eq 9 end @@ -151,8 +150,8 @@ def collection end it 'should increment each scope separately' do - 3.times.each{ |n| Recipe.create(chef: 'Jack', name: n + 1) } - 9.times.each{ |n| Recipe.create(chef: 'Jill', name: n + 1) } + 3.times.each { |n| Recipe.create(chef: 'Jack', name: n + 1) } + 9.times.each { |n| Recipe.create(chef: 'Jill', name: n + 1) } expect(Recipe.find_by(chef: 'Jack', name: 3).rank).to eq 3 expect(Recipe.find_by(chef: 'Jill', name: 9).rank).to eq 9 end @@ -171,9 +170,9 @@ def collection end it 'should increment each scope separately' do - 2.times.each{ |n| Ingredient.create(recipe: @recipe1, type: 'greens', name: n + 1) } - 3.times.each{ |n| Ingredient.create(recipe: @recipe1, type: 'meats', name: n + 1) } - 5.times.each{ |n| Ingredient.create(recipe: @recipe2, type: 'greens', name: n + 1) } + 2.times.each { |n| Ingredient.create(recipe: @recipe1, type: 'greens', name: n + 1) } + 3.times.each { |n| Ingredient.create(recipe: @recipe1, type: 'meats', name: n + 1) } + 5.times.each { |n| Ingredient.create(recipe: @recipe2, type: 'greens', name: n + 1) } expect(Ingredient.find_by(recipe: @recipe1, type: 'greens', name: 2).cost).to eq 2 expect(Ingredient.find_by(recipe: @recipe1, type: 'meats', name: 3).cost).to eq 3 expect(Ingredient.find_by(recipe: @recipe2, type: 'greens', name: 5).cost).to eq 5 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b584f13..ffeedd7 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,13 +1,13 @@ $LOAD_PATH.unshift(File.dirname(__FILE__)) -$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib")) +$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) -MODELS = File.join(File.dirname(__FILE__), "models") +MODELS = File.join(File.dirname(__FILE__), 'models') -require "rubygems" -require "mongoid" -require "mongoid_auto_increment" -require "database_cleaner" -require "simplecov" +require 'rubygems' +require 'mongoid' +require 'mongoid_auto_increment' +require 'database_cleaner' +require 'simplecov' SimpleCov.start @@ -15,14 +15,14 @@ if defined?(::Mongoid::VERSION) && ::Mongoid::VERSION > '3' Mongoid.configure do |config| - config.connect_to "mongoid_auto_increment_test" + config.connect_to 'mongoid_auto_increment_test' end else - Mongoid.config.master = Mongo::Connection.new.db("mongoid_auto_increment_test") + Mongoid.config.master = Mongo::Connection.new.db('mongoid_auto_increment_test') end Mongoid.logger = Logger.new($stdout) -DatabaseCleaner.orm = "mongoid" +DatabaseCleaner.orm = 'mongoid' RSpec.configure do |config| config.before(:all) do @@ -36,4 +36,4 @@ config.after(:each) do DatabaseCleaner.clean end -end \ No newline at end of file +end