Skip to content

Commit

Permalink
Merge pull request #17 from proton/rubocop
Browse files Browse the repository at this point in the history
Rubocop-generated syntax changes
  • Loading branch information
proton authored Nov 6, 2018
2 parents bbf6891 + a64c4aa commit dd6a06e
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 81 deletions.
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
40 changes: 19 additions & 21 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# encoding: utf-8

require 'rubygems'
require 'rake'
require 'bundler'
Expand All @@ -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 = "[email protected]"
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 = '[email protected]'
gem.authors = ['Peter Savichev (proton)']
# dependencies defined in Gemfile
end
Jeweler::RubygemsDotOrgTasks.new
Expand All @@ -33,27 +31,27 @@ 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}"
rdoc.rdoc_files.include('README*')
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
4 changes: 2 additions & 2 deletions lib/mongoid_auto_increment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 14 additions & 15 deletions lib/mongoid_auto_increment/incrementor.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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])
Expand Down
3 changes: 1 addition & 2 deletions spec/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ class Invoice

field :name

auto_increment :num, :seed => 1000, :step => 5
auto_increment :num, seed: 1000, step: 5
end

2 changes: 1 addition & 1 deletion spec/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ class Order

field :title

auto_increment :num, :seed => 1000
auto_increment :num, seed: 1000
end
2 changes: 1 addition & 1 deletion spec/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Post

field :title

auto_increment :key, :seed => 500
auto_increment :key, seed: 500
auto_increment :num

embeds_many :comments
Expand Down
51 changes: 25 additions & 26 deletions spec/mongoid_auto_increment_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
22 changes: 11 additions & 11 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
$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

Dir["#{MODELS}/*.rb"].each { |f| require f }

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
Expand All @@ -36,4 +36,4 @@
config.after(:each) do
DatabaseCleaner.clean
end
end
end

0 comments on commit dd6a06e

Please sign in to comment.