Skip to content

Commit c7f77b6

Browse files
author
Ricard Forniol
committed
Fix rubocop errors
1 parent d53a1f8 commit c7f77b6

File tree

15 files changed

+148
-104
lines changed

15 files changed

+148
-104
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: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Mongoid
24
module Token
35
extend ActiveSupport::Concern
@@ -24,9 +26,10 @@ def resolvers
2426
end
2527

2628
private
29+
2730
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 })
31+
field options.field_name, type: String, default: default_value(options)
32+
index({ options.field_name => 1 }, unique: true, sparse: true)
3033
end
3134

3235
def add_token_collision_resolver(options)
@@ -55,24 +58,26 @@ def set_token_callbacks(options)
5558
end
5659

5760
def override_to_param(options)
58-
self.send(:define_method, :to_param) do
59-
self.send(options.field_name) || super()
61+
send(:define_method, :to_param) do
62+
send(options.field_name) || super()
6063
end
6164
end
6265

6366
def default_value(options)
64-
options.generate_on_init && Mongoid::Token::Generator.generate(options.pattern) || nil
67+
options.generate_on_init &&
68+
Mongoid::Token::Generator.generate(options.pattern) || nil
6569
end
6670
end
6771

6872
protected
73+
6974
def create_token(field_name, pattern)
70-
self.send :"#{field_name.to_s}=", self.generate_token(pattern)
75+
send :"#{field_name}=", generate_token(pattern)
7176
end
7277

7378
def create_token_if_nil(field_name, pattern)
7479
if self[field_name.to_sym].blank?
75-
self.create_token field_name, pattern
80+
create_token field_name, pattern
7681
end
7782
end
7883

lib/mongoid/token/collision_resolver.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
module Mongoid
24
module Token
35
module SafeOperationsHandler

lib/mongoid/token/collision_retries_exceeded.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Mongoid
24
module Token
35
class CollisionRetriesExceeded < Error
@@ -7,7 +9,8 @@ def initialize(resource = "unknown resource", attempts = "unspecified")
79
end
810

911
def to_s
10-
"Failed to generate unique token for #{@resource} after #{@attempts} attempts."
12+
"Failed to generate unique token for #{@resource} after #{@attempts} "\
13+
"attempts."
1114
end
1215
end
1316
end

lib/mongoid/token/collisions.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Mongoid
24
module Token
35
module Collisions
@@ -10,6 +12,7 @@ def resolve_token_collisions
1012
duplicate_token_error?(e, self, r.field_name)
1113
end.first
1214
raise e unless resolver
15+
1316
retries ||= resolver.retry_count
1417
if (retries -= 1) >= 0
1518
resolver.create_new_token_for(self)
@@ -21,12 +24,16 @@ def resolve_token_collisions
2124
end
2225

2326
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)
27+
if defined?(Rails)
28+
Rails.logger.warn "[Mongoid::Token] Warning: Maximum token "\
29+
"generation retries (#{retry_count}) exceeded on "\
30+
"`#{field_name}'."
31+
end
2532
raise Mongoid::Token::CollisionRetriesExceeded.new(self, retry_count)
2633
end
2734

2835
def duplicate_token_error?(err, document, field_name)
29-
[11000, 11001].include?(err.code) &&
36+
[11_000, 11_001].include?(err.code) &&
3037
err.message =~ /dup key/ &&
3138
err.message =~ /"#{document.send(field_name)}"/ &&
3239
true

lib/mongoid/token/error.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
module Mongoid
24
module Token
35
class Error < StandardError; end

lib/mongoid/token/finders.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Mongoid
24
module Token
35
module Finders
@@ -6,7 +8,7 @@ def self.define_custom_token_finder_for(klass, field_name = :token)
68
if token.is_a?(Array)
79
self.in field_name.to_sym => token
810
else
9-
self.find_by field_name.to_sym => token
11+
find_by field_name.to_sym => token
1012
end
1113
end
1214
end

lib/mongoid/token/generator.rb

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# frozen_string_literal: true
12
# proposed pattern options
23
# %c - lowercase character
34
# %C - uppercase character
@@ -7,73 +8,73 @@
78
# %w - upper and lower alpha character
89
# %p - URL-safe punctuation
910
#
10-
# Any pattern can be followed by a number, representing how many of that type to generate
11+
# Any pattern can be followed by a number, representing how many of that type
12+
# to generate
1113

1214
module Mongoid
1315
module Token
1416
module Generator
1517
REPLACE_PATTERN = /%((?<character>[cCdDhHpsw]{1})(?<length>\d+(,\d+)?)?)/
18+
TYPES = {
19+
c: ->(length) { down_character(length) },
20+
C: ->(length) { up_character(length) },
21+
d: ->(length) { digits(length) },
22+
D: ->(length) { integer(length) },
23+
h: ->(length) { digits(length, 16) },
24+
H: ->(length) { integer(length, 16) },
25+
s: ->(length) { alphanumeric(length) },
26+
w: ->(length) { alpha(length) },
27+
p: ->(_length) { "-" }
28+
}.freeze
1629

1730
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
31+
pattern.gsub REPLACE_PATTERN do
32+
match = $~
33+
type = match[:character]
34+
length = [match[:length].to_i, 1].max
2235

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
36+
TYPES[type.to_sym].call(length)
4337
end
4438
end
4539

46-
private
4740
def self.rand_string_from_chars(chars, length = 1)
48-
Array.new(length).map{ chars.sample }.join
41+
Array.new(length).map { chars.sample }.join
4942
end
5043

5144
def self.down_character(length = 1)
52-
self.rand_string_from_chars ('a'..'z').to_a, length
45+
rand_string_from_chars ("a".."z").to_a, length
5346
end
5447

5548
def self.up_character(length = 1)
56-
self.rand_string_from_chars ('A'..'Z').to_a, length
49+
rand_string_from_chars ("A".."Z").to_a, length
5750
end
5851

5952
def self.integer(length = 1, base = 10)
60-
(rand(base**length - base**(length-1)) + base**(length-1)).to_s(base)
53+
(rand(base**length - base**(length - 1)) + base**(length - 1)).
54+
to_s(base)
6155
end
6256

6357
def self.digits(length = 1, base = 10)
6458
rand(base**length).to_s(base).rjust(length, "0")
6559
end
6660

6761
def self.alpha(length = 1)
68-
self.rand_string_from_chars (('A'..'Z').to_a + ('a'..'z').to_a), length
62+
rand_string_from_chars (("A".."Z").to_a + ("a".."z").to_a), length
6963
end
7064

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

7576
def self.punctuation(length = 1)
76-
self.rand_string_from_chars ['.','-','_','=','+','$'], length
77+
rand_string_from_chars %w[. - _ = + $], length
7778
end
7879
end
7980
end

lib/mongoid/token/options.rb

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1+
# frozen_string_literal: true
2+
13
module Mongoid
24
module Token
35
class Options
6+
PATTERNS = {
7+
alphanumeric: ->(length) { "%s#{length}" },
8+
alpha: ->(length) { "%w#{length}" },
9+
alpha_upper: ->(length) { "%C#{length}" },
10+
alpha_lower: ->(length) { "%c#{length}" },
11+
numeric: ->(length) { "%d1,#{length}" },
12+
fixed_numeric: ->(length) { "%d#{length}" },
13+
fixed_numeric_no_leading_zeros: ->(length) { "%D#{length}" },
14+
fixed_hex_numeric: ->(length) { "%h#{length}" },
15+
fixed_hex_numeric_no_leading_zeros: ->(length) { "%H#{length}" }
16+
}.freeze
17+
418
def initialize(options = {})
519
@options = merge_defaults validate_options(options)
620
end
@@ -34,32 +48,17 @@ def generate_on_init
3448
end
3549

3650
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
51+
@options[:pattern] ||=
52+
PATTERNS[@options[:contains]].call(@options[:length])
5753
end
5854

5955
private
56+
6057
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"
58+
if options.key?(:retry)
59+
warn "Mongoid::Token Deprecation Warning: option `retry` has "\
60+
"been renamed to `retry_count`. `:retry` will be "\
61+
"removed in v2.1"
6362
options[:retry_count] = options[:retry]
6463
end
6564
options

lib/mongoid_token.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
require 'zeitwerk'
2-
require 'active_support/concern'
1+
# frozen_string_literal: true
2+
3+
require "zeitwerk"
4+
require "active_support/concern"
35
loader = Zeitwerk::Loader.for_gem
46
loader.setup

0 commit comments

Comments
 (0)