Skip to content

Commit 34531b4

Browse files
author
Ricard Forniol
committed
Fix rubocop errors
1 parent 1246714 commit 34531b4

File tree

10 files changed

+81
-70
lines changed

10 files changed

+81
-70
lines changed

Gemfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ source "http://rubygems.org"
33
gemspec
44

55
group :test do
6-
gem 'rspec'
76
gem "codeclimate-test-reporter", require: nil
8-
gem 'database_cleaner'
9-
gem 'mongoid-rspec'
7+
gem "database_cleaner"
8+
gem "mongoid-rspec"
9+
gem "rspec"
1010
end

lib/mongoid/token.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ def resolvers
2424
end
2525

2626
private
27+
2728
def add_token_field_and_index(options)
28-
self.field options.field_name, :type => String, :default => default_value(options)
29-
self.index({ options.field_name => 1 }, { :unique => true, :sparse => true })
29+
field options.field_name, type: String, default: default_value(options)
30+
index({ options.field_name => 1 }, unique: true, sparse: true)
3031
end
3132

3233
def add_token_collision_resolver(options)
@@ -55,24 +56,26 @@ def set_token_callbacks(options)
5556
end
5657

5758
def override_to_param(options)
58-
self.send(:define_method, :to_param) do
59-
self.send(options.field_name) || super()
59+
send(:define_method, :to_param) do
60+
send(options.field_name) || super()
6061
end
6162
end
6263

6364
def default_value(options)
64-
options.generate_on_init && Mongoid::Token::Generator.generate(options.pattern) || nil
65+
options.generate_on_init &&
66+
Mongoid::Token::Generator.generate(options.pattern) || nil
6567
end
6668
end
6769

6870
protected
71+
6972
def create_token(field_name, pattern)
70-
self.send :"#{field_name.to_s}=", self.generate_token(pattern)
73+
send :"#{field_name}=", generate_token(pattern)
7174
end
7275

7376
def create_token_if_nil(field_name, pattern)
7477
if self[field_name.to_sym].blank?
75-
self.create_token field_name, pattern
78+
create_token field_name, pattern
7679
end
7780
end
7881

lib/mongoid/token/collision_retries_exceeded.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ def initialize(resource = "unknown resource", attempts = "unspecified")
77
end
88

99
def to_s
10-
"Failed to generate unique token for #{@resource} after #{@attempts} attempts."
10+
"Failed to generate unique token for #{@resource} after #{@attempts} "\
11+
"attempts."
1112
end
1213
end
1314
end

lib/mongoid/token/collisions.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def resolve_token_collisions
1010
duplicate_token_error?(e, self, r.field_name)
1111
end.first
1212
raise e unless resolver
13+
1314
retries ||= resolver.retry_count
1415
if (retries -= 1) >= 0
1516
resolver.create_new_token_for(self)
@@ -21,12 +22,16 @@ def resolve_token_collisions
2122
end
2223

2324
def raise_collision_retries_exceeded_error(field_name, retry_count)
24-
Rails.logger.warn "[Mongoid::Token] Warning: Maximum token generation retries (#{retry_count}) exceeded on `#{field_name}'." if defined?(Rails)
25+
if defined?(Rails)
26+
Rails.logger.warn "[Mongoid::Token] Warning: Maximum token "\
27+
"generation retries (#{retry_count}) exceeded on "\
28+
"`#{field_name}'."
29+
end
2530
raise Mongoid::Token::CollisionRetriesExceeded.new(self, retry_count)
2631
end
2732

2833
def duplicate_token_error?(err, document, field_name)
29-
[11000, 11001].include?(err.code) &&
34+
[11_000, 11_001].include?(err.code) &&
3035
err.message =~ /dup key/ &&
3136
err.message =~ /"#{document.send(field_name)}"/ &&
3237
true

lib/mongoid/token/finders.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def self.define_custom_token_finder_for(klass, field_name = :token)
66
if token.is_a?(Array)
77
self.in field_name.to_sym => token
88
else
9-
self.find_by field_name.to_sym => token
9+
find_by field_name.to_sym => token
1010
end
1111
end
1212
end

lib/mongoid/token/generator.rb

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,73 +7,76 @@
77
# %w - upper and lower alpha character
88
# %p - URL-safe punctuation
99
#
10-
# Any pattern can be followed by a number, representing how many of that type to generate
10+
# Any pattern can be followed by a number, representing how many of that type
11+
# to generate
1112

1213
module Mongoid
1314
module Token
1415
module Generator
1516
REPLACE_PATTERN = /%((?<character>[cCdDhHpsw]{1})(?<length>\d+(,\d+)?)?)/
1617

1718
def self.generate(pattern)
18-
pattern.gsub REPLACE_PATTERN do |match|
19-
match_data = $~
20-
type = match_data[:character]
21-
length = [match_data[:length].to_i, 1].max
19+
pattern.gsub REPLACE_PATTERN do
20+
match = $~
21+
type = match[:character]
22+
length = [match[:length].to_i, 1].max
2223

23-
case type
24-
when 'c'
25-
down_character(length)
26-
when 'C'
27-
up_character(length)
28-
when 'd'
29-
digits(length)
30-
when 'D'
31-
integer(length)
32-
when 'h'
33-
digits(length, 16)
34-
when 'H'
35-
integer(length, 16)
36-
when 's'
37-
alphanumeric(length)
38-
when 'w'
39-
alpha(length)
40-
when 'p'
41-
"-"
42-
end
24+
type_for(type, length)
25+
end
26+
end
27+
28+
def self.type_for(type, length)
29+
case type
30+
when "c" then down_character(length)
31+
when "C" then up_character(length)
32+
when "d" then digits(length)
33+
when "D" then integer(length)
34+
when "h" then digits(length, 16)
35+
when "H" then integer(length, 16)
36+
when "s" then alphanumeric(length)
37+
when "w" then alpha(length)
38+
when "p" then "-"
4339
end
4440
end
4541

46-
private
4742
def self.rand_string_from_chars(chars, length = 1)
48-
Array.new(length).map{ chars.sample }.join
43+
Array.new(length).map { chars.sample }.join
4944
end
5045

5146
def self.down_character(length = 1)
52-
self.rand_string_from_chars ('a'..'z').to_a, length
47+
rand_string_from_chars ("a".."z").to_a, length
5348
end
5449

5550
def self.up_character(length = 1)
56-
self.rand_string_from_chars ('A'..'Z').to_a, length
51+
rand_string_from_chars ("A".."Z").to_a, length
5752
end
5853

5954
def self.integer(length = 1, base = 10)
60-
(rand(base**length - base**(length-1)) + base**(length-1)).to_s(base)
55+
(rand(base**length - base**(length - 1)) + base**(length - 1)).
56+
to_s(base)
6157
end
6258

6359
def self.digits(length = 1, base = 10)
6460
rand(base**length).to_s(base).rjust(length, "0")
6561
end
6662

6763
def self.alpha(length = 1)
68-
self.rand_string_from_chars (('A'..'Z').to_a + ('a'..'z').to_a), length
64+
rand_string_from_chars (("A".."Z").to_a + ("a".."z").to_a), length
6965
end
7066

7167
def self.alphanumeric(length = 1)
72-
(1..length).collect { (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr }.join
68+
(1..length).map do
69+
i = Kernel.rand(62)
70+
if i < 10
71+
i + 48
72+
else
73+
i + (i < 36 ? 55 : 61)
74+
end.chr
75+
end.join
7376
end
7477

7578
def self.punctuation(length = 1)
76-
self.rand_string_from_chars ['.','-','_','=','+','$'], length
79+
rand_string_from_chars %w[. - _ = + $], length
7780
end
7881
end
7982
end

lib/mongoid/token/options.rb

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,32 +34,27 @@ def generate_on_init
3434
end
3535

3636
def pattern
37-
@options[:pattern] ||= case @options[:contains].to_sym
38-
when :alphanumeric
39-
"%s#{@options[:length]}"
40-
when :alpha
41-
"%w#{@options[:length]}"
42-
when :alpha_upper
43-
"%C#{@options[:length]}"
44-
when :alpha_lower
45-
"%c#{@options[:length]}"
46-
when :numeric
47-
"%d1,#{@options[:length]}"
48-
when :fixed_numeric
49-
"%d#{@options[:length]}"
50-
when :fixed_numeric_no_leading_zeros
51-
"%D#{@options[:length]}"
52-
when :fixed_hex_numeric
53-
"%h#{@options[:length]}"
54-
when :fixed_hex_numeric_no_leading_zeros
55-
"%H#{@options[:length]}"
56-
end
37+
@options[:pattern] ||=
38+
case @options[:contains].to_sym
39+
when :alphanumeric then "%s#{@options[:length]}"
40+
when :alpha then "%w#{@options[:length]}"
41+
when :alpha_upper then "%C#{@options[:length]}"
42+
when :alpha_lower then "%c#{@options[:length]}"
43+
when :numeric then "%d1,#{@options[:length]}"
44+
when :fixed_numeric then "%d#{@options[:length]}"
45+
when :fixed_numeric_no_leading_zeros then "%D#{@options[:length]}"
46+
when :fixed_hex_numeric then "%h#{@options[:length]}"
47+
when :fixed_hex_numeric_no_leading_zeros then "%H#{@options[:length]}"
48+
end
5749
end
5850

5951
private
52+
6053
def validate_options(options)
61-
if options.has_key?(:retry)
62-
STDERR.puts "Mongoid::Token Deprecation Warning: option `retry` has been renamed to `retry_count`. `:retry` will be removed in v2.1"
54+
if options.key?(:retry)
55+
warn "Mongoid::Token Deprecation Warning: option `retry` has "\
56+
"been renamed to `retry_count`. `:retry` will be "\
57+
"removed in v2.1"
6358
options[:retry_count] = options[:retry]
6459
end
6560
options

spec/mongoid/token/collisions_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
allow(document).to receive("token").and_return("tokenvalue123")
108108
allow(err).to(receive("message").and_return(message))
109109
end
110-
let(:err) { double('Mongo::Error::OperationFailure', code: 11000) }
110+
let(:err) { double('Mongo::Error::OperationFailure', code: 11_000) }
111111
subject { document.duplicate_token_error?(err, document, :token) }
112112

113113
context "mongodb version 2.6, 3.0" do

spec/mongoid/token_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require File.join(File.dirname(__FILE__), %w[.. spec_helper])
24

35
describe Mongoid::Token do

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require "simplecov"
24
require "codeclimate-test-reporter"
35
SimpleCov.start

0 commit comments

Comments
 (0)