Skip to content

Commit

Permalink
Merge pull request #426 from toptal/rubocop
Browse files Browse the repository at this point in the history
Rubocop
  • Loading branch information
pyromaniac authored Sep 7, 2016
2 parents bf8a74b + 541cae1 commit e4af9da
Show file tree
Hide file tree
Showing 22 changed files with 186 additions and 243 deletions.
116 changes: 7 additions & 109 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,146 +1,44 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2016-09-06 12:08:00 +0700 using RuboCop version 0.42.0.
# on 2016-09-07 10:18:40 +0700 using RuboCop version 0.42.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 41
Metrics/AbcSize:
Max: 47
Max: 48

# Offense count: 4
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 271
Max: 274

# Offense count: 14
Metrics/CyclomaticComplexity:
Max: 13

# Offense count: 1455
# Offense count: 1462
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
# URISchemes: http, https
Metrics/LineLength:
Max: 290

# Offense count: 36
# Offense count: 38
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 32
Max: 33

# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 170
Max: 172

# Offense count: 14
Metrics/PerceivedComplexity:
Max: 15

# Offense count: 2
# Cop supports --auto-correct.
Performance/EndWith:
Exclude:
- 'lib/chewy/type/wrapper.rb'

# Offense count: 2
Style/AsciiComments:
Exclude:
- 'lib/chewy/type/mapping.rb'
- 'spec/chewy_spec.rb'

# Offense count: 1
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: nested, compact
Style/ClassAndModuleChildren:
Exclude:
- 'lib/chewy.rb'

# Offense count: 93
Style/Documentation:
Enabled: false

# Offense count: 2
# Configuration parameters: MinBodyLength.
Style/GuardClause:
Exclude:
- 'lib/chewy/fields/root.rb'
- 'lib/chewy/query/nodes/has_relation.rb'

# Offense count: 2
Style/IfInsideElse:
Exclude:
- 'lib/chewy/rspec/update_index.rb'

# Offense count: 2
Style/IfUnlessModifierOfIfUnless:
Exclude:
- 'lib/chewy/type/witchcraft.rb'

# Offense count: 3
Style/MethodMissing:
Exclude:
- 'lib/chewy/query/filters.rb'
- 'lib/chewy/query/nodes/field.rb'
- 'lib/chewy/type.rb'

# Offense count: 1
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: snake_case, camelCase
Style/MethodName:
Exclude:
- 'spec/chewy/query/filters_spec.rb'

# Offense count: 3
Style/MultilineBlockChain:
Exclude:
- 'lib/chewy/type/import.rb'
- 'lib/chewy/type/observe.rb'

# Offense count: 7
Style/MultilineTernaryOperator:
Exclude:
- 'lib/chewy/fields/base.rb'
- 'lib/chewy/query.rb'
- 'lib/chewy/query/nodes/regexp.rb'
- 'lib/chewy/type/adapter/object.rb'
- 'lib/chewy/type/adapter/orm.rb'
- 'lib/chewy/type/observe.rb'

# Offense count: 2
Style/NestedTernaryOperator:
Exclude:
- 'lib/chewy/fields/base.rb'
- 'lib/chewy/type/adapter/orm.rb'

# Offense count: 3
# Cop supports --auto-correct.
Style/NilComparison:
Exclude:
- 'spec/chewy/query/filters_spec.rb'
- 'spec/chewy/query/nodes/missing_spec.rb'

# Offense count: 4
# Cop supports --auto-correct.
# Configuration parameters: IncludeSemanticChanges.
Style/NonNilCheck:
Exclude:
- 'spec/chewy/query/filters_spec.rb'
- 'spec/chewy/query/nodes/exists_spec.rb'

# Offense count: 8
Style/OpMethod:
Exclude:
- 'lib/chewy/query/nodes/field.rb'

# Offense count: 2
# Configuration parameters: NamePrefix, NamePrefixBlacklist, NameWhitelist.
# NamePrefix: is_, has_, have_
# NamePrefixBlacklist: is_, has_, have_
# NameWhitelist: is_a?
Style/PredicateName:
Exclude:
- 'spec/**/*'
- 'lib/chewy/query/filters.rb'
8 changes: 6 additions & 2 deletions lib/chewy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@
end

ActiveSupport.on_load(:mongoid) do
module Mongoid::Document::ClassMethods
include Chewy::Type::Observe::MongoidMethods
module Mongoid
module Document
module ClassMethods
include Chewy::Type::Observe::MongoidMethods
end
end
end

begin
Expand Down
8 changes: 5 additions & 3 deletions lib/chewy/fields/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ def object_field?
end

def mappings_hash
mapping = children.present? ? {
(multi_field? ? :fields : :properties) => children.map(&:mappings_hash).inject(:merge)
} : {}
mapping = if children.present?
{ (multi_field? ? :fields : :properties) => children.map(&:mappings_hash).inject(:merge) }
else
{}
end
mapping.reverse_merge!(options)
mapping.reverse_merge!(type: (children.present? ? 'object' : 'string'))
{ name => mapping }
Expand Down
10 changes: 4 additions & 6 deletions lib/chewy/fields/root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,13 @@ def dynamic_template(*args)
end

def compose_parent(object)
if parent_id
parent_id.arity.zero? ? object.instance_exec(&parent_id) : parent_id.call(object)
end
return unless parent_id
parent_id.arity.zero? ? object.instance_exec(&parent_id) : parent_id.call(object)
end

def compose_id(object)
if id
id.arity.zero? ? object.instance_exec(&id) : id.call(object)
end
return unless id
id.arity.zero? ? object.instance_exec(&id) : id.call(object)
end
end
end
Expand Down
7 changes: 5 additions & 2 deletions lib/chewy/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1045,8 +1045,11 @@ def _results
def _collection
@_collection ||= begin
_load_objects! if criteria.options[:preload]
criteria.options[:preload] && criteria.options[:loaded_objects] ?
_results.map(&:_object) : _results
if criteria.options[:preload] && criteria.options[:loaded_objects]
_results.map(&:_object)
else
_results
end
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/chewy/query/filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def r(raw = nil, &block)
# .filter_mode(:or)
# end
#
def has_child(type)
def has_child(type) # rubocop:disable Style/PredicateName
Nodes::HasChild.new(type, @outer)
end

Expand All @@ -177,7 +177,7 @@ def has_child(type)
# .filter_mode(:or)
# end
#
def has_parent(type)
def has_parent(type) # rubocop:disable Style/PredicateName
Nodes::HasParent.new(type, @outer)
end

Expand All @@ -200,7 +200,7 @@ def match_all
# UsersIndex.filter{ article.title =~ 'Hello' }
# UsersIndex.filter{ article.tags? }
#
def method_missing(method, *args)
def method_missing(method, *args) # rubocop:disable Style/MethodMissing
method = method.to_s
if method =~ /\?\Z/
Nodes::Exists.new method.gsub(/\?\Z/, '')
Expand Down
56 changes: 30 additions & 26 deletions lib/chewy/query/nodes/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,65 +16,69 @@ def ~
self
end

def >(value)
Nodes::Range.new @name, *__options_merge__(gt: value)
def >(other)
Nodes::Range.new @name, *__options_merge__(gt: other)
end

def <(value)
Nodes::Range.new @name, *__options_merge__(lt: value)
def <(other)
Nodes::Range.new @name, *__options_merge__(lt: other)
end

def >=(value)
Nodes::Range.new @name, *__options_merge__(gt: value, left_closed: true)
def >=(other)
Nodes::Range.new @name, *__options_merge__(gt: other, left_closed: true)
end

def <=(value)
Nodes::Range.new @name, *__options_merge__(lt: value, right_closed: true)
def <=(other)
Nodes::Range.new @name, *__options_merge__(lt: other, right_closed: true)
end

def ==(value)
case value
def ==(other)
case other
when nil
Nodes::Missing.new @name, existence: false, null_value: true
nil?
when ::Regexp
Nodes::Regexp.new @name, value, *@args
Nodes::Regexp.new @name, other, *@args
when ::Range
Nodes::Range.new @name, *__options_merge__(gt: value.first, lt: value.last)
Nodes::Range.new @name, *__options_merge__(gt: other.first, lt: other.last)
else
if value.is_a?(Array) && value.first.is_a?(::Range)
if other.is_a?(Array) && other.first.is_a?(::Range)
Nodes::Range.new @name, *__options_merge__(
gt: value.first.first, lt: value.first.last,
gt: other.first.first, lt: other.first.last,
left_closed: true, right_closed: true
)
else
Nodes::Equal.new @name, value, *@args
Nodes::Equal.new @name, other, *@args
end
end
end

def !=(value)
case value
def !=(other)
case other
when nil
Nodes::Exists.new @name
else
Nodes::Not.new self == value
Nodes::Not.new self == other
end
end

def =~(value)
case value
def =~(other)
case other
when ::Regexp
Nodes::Regexp.new @name, value, *@args
Nodes::Regexp.new @name, other, *@args
else
Nodes::Prefix.new @name, value, @args.extract_options!
Nodes::Prefix.new @name, other, @args.extract_options!
end
end

def !~(value)
Not.new(self =~ value)
def !~(other)
Not.new(self =~ other)
end

def method_missing(method, *args)
def nil?
Nodes::Missing.new @name, existence: false, null_value: true
end

def method_missing(method, *args) # rubocop:disable Style/MethodMissing
method = method.to_s
if method =~ /\?\Z/
Nodes::Exists.new [@name, method.gsub(/\?\Z/, '')].join('.')
Expand Down
7 changes: 2 additions & 5 deletions lib/chewy/query/nodes/has_relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ def filter_mode(mode)
end

def query(params = nil, &block)
if block
raise 'Query DLS is not supported yet'
else
@queries.push(params)
end
raise 'Query DLS is not supported yet' if block
@queries.push(params)
self
end

Expand Down
13 changes: 9 additions & 4 deletions lib/chewy/query/nodes/regexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@ def initialize(name, regexp, *args)
end

def __render__
body = @options[:flags] ?
{ value: @regexp, flags: @options[:flags].map(&:to_s).map(&:upcase).uniq.join('|') } :
body = if @options[:flags]
{ value: @regexp, flags: @options[:flags].map(&:to_s).map(&:upcase).uniq.join('|') }
else
@regexp
end
filter = { @name => body }
if @options.key?(:cache)
filter[:_cache] = !!@options[:cache]
filter[:_cache_key] = @options[:cache].is_a?(TrueClass) || @options[:cache].is_a?(FalseClass) ?
@regexp.underscore : @options[:cache]
filter[:_cache_key] = if @options[:cache].is_a?(TrueClass) || @options[:cache].is_a?(FalseClass)
@regexp.underscore
else
@options[:cache]
end
end
{ regexp: filter }
end
Expand Down
8 changes: 4 additions & 4 deletions lib/chewy/rspec/update_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ def supports_block_expectations?
if (document = @reindex[body[:_id].to_s])
document[:real_count] += 1
document[:real_attributes].merge!(body[:data])
else
@missed_reindex.push(body[:_id].to_s) if @only
elsif @only
@missed_reindex.push(body[:_id].to_s)
end
elsif (body = updated_document[:delete])
if (document = @delete[body[:_id].to_s])
document[:real_count] += 1
else
@missed_delete.push(body[:_id].to_s) if @only
elsif @only
@missed_delete.push(body[:_id].to_s)
end
end
end
Expand Down
Loading

0 comments on commit e4af9da

Please sign in to comment.