Skip to content

Commit

Permalink
Merge pull request #423 from toptal/rubocop
Browse files Browse the repository at this point in the history
Rubocop
  • Loading branch information
pyromaniac authored Sep 6, 2016
2 parents 9e3134c + 7a97790 commit bf8a74b
Show file tree
Hide file tree
Showing 122 changed files with 2,619 additions and 2,505 deletions.
575 changes: 7 additions & 568 deletions .rubocop_todo.yml

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions Guardfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
guard :rspec, cmd: 'rspec' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
watch('spec/spec_helper.rb') { 'spec' }

# Rails example
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
watch('config/routes.rb') { "spec/routing" }
watch('app/controllers/application_controller.rb') { "spec/controllers" }
watch(%r{^spec/support/(.+)\.rb$}) { 'spec' }
watch('config/routes.rb') { 'spec/routing' }
watch('app/controllers/application_controller.rb') { 'spec/controllers' }

# Capybara features specs
watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" }

# Turnip features and steps
watch(%r{^spec/acceptance/(.+)\.feature$})
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
end
15 changes: 7 additions & 8 deletions lib/chewy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ module Mongoid::Document::ClassMethods
end

module Chewy

@adapters = [
Chewy::Type::Adapter::ActiveRecord,
Chewy::Type::Adapter::Mongoid,
Expand All @@ -94,25 +93,25 @@ class << self
#
# If index has more then one type - it raises Chewy::UnderivableType.
#
def derive_type name
def derive_type(name)
return name if name.is_a?(Class) && name < Chewy::Type

index_name, type_name = name.split('#', 2)
class_name = "#{index_name.camelize}Index"
index = class_name.safe_constantize
raise Chewy::UnderivableType.new("Can not find index named `#{class_name}`") unless index && index < Chewy::Index
raise Chewy::UnderivableType, "Can not find index named `#{class_name}`" unless index && index < Chewy::Index
if type_name.present?
index.type_hash[type_name] or raise Chewy::UnderivableType.new("Index `#{class_name}` doesn`t have type named `#{type_name}`")
index.type_hash[type_name] or raise Chewy::UnderivableType, "Index `#{class_name}` doesn`t have type named `#{type_name}`"
elsif index.types.one?
index.types.first
else
raise Chewy::UnderivableType.new("Index `#{class_name}` has more than one type, please specify type via `#{index_name}#type_name`")
raise Chewy::UnderivableType, "Index `#{class_name}` has more than one type, please specify type via `#{index_name}#type_name`"
end
end

# Creates Chewy::Type ancestor defining index and adapter methods.
#
def create_type index, target, options = {}, &block
def create_type(index, target, options = {}, &block)
type = Class.new(Chewy::Type)

adapter = adapters.find { |klass| klass.accepts?(target) }.new(target, options)
Expand Down Expand Up @@ -149,7 +148,7 @@ def wait_for_status
# Be careful, if current prefix is blank, this will destroy all the indexes.
#
def massacre
Chewy.client.indices.delete(index: [Chewy.configuration[:prefix], '*'].delete_if(&:blank?).join(?_))
Chewy.client.indices.delete(index: [Chewy.configuration[:prefix], '*'].delete_if(&:blank?).join('_'))
Chewy.wait_for_status
end
alias_method :delete_all, :massacre
Expand Down Expand Up @@ -177,7 +176,7 @@ def massacre
# Chewy.strategy.pop
# city3.do_update! # index updated again
#
def strategy name = nil, &block
def strategy(name = nil, &block)
Thread.current[:chewy_strategy] ||= Chewy::Strategy.new
if name
if block
Expand Down
2 changes: 1 addition & 1 deletion lib/chewy/backports/deep_dup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Array
# array[1][2] # => nil
# dup[1][2] # => 4
def deep_dup
map { |it| it.deep_dup }
map(&:deep_dup)
end
end

Expand Down
8 changes: 4 additions & 4 deletions lib/chewy/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Config
:indices_path

def self.delegated
public_instance_methods - self.superclass.public_instance_methods - Singleton.public_instance_methods
public_instance_methods - superclass.public_instance_methods - Singleton.public_instance_methods
end

def initialize
Expand All @@ -56,12 +56,12 @@ def initialize
@indices_path = 'app/chewy'
end

def transport_logger= logger
def transport_logger=(logger)
Chewy.client.transport.logger = logger
@transport_logger = logger
end

def transport_tracer= tracer
def transport_tracer=(tracer)
Chewy.client.transport.tracer = tracer
@transport_tracer = tracer
end
Expand Down Expand Up @@ -116,7 +116,7 @@ def configuration
end
end

def configuration= options
def configuration=(options)
ActiveSupport::Deprecation.warn("`Chewy.configuration = {foo: 'bar'}` method is deprecated and will be removed soon, use `Chewy.settings = {foo: 'bar'}` method instead")
self.settings = options
end
Expand Down
4 changes: 2 additions & 2 deletions lib/chewy/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class UnderivableType < Error
end

class UndefinedUpdateStrategy < Error
def initialize _type
def initialize(_type)
super <<-MESSAGE
Index update strategy is undefined in current context.
Please wrap your code with `Chewy.strategy(:strategy_name) block.`
Expand All @@ -24,7 +24,7 @@ class DocumentNotFound < Error
end

class ImportFailed < Error
def initialize type, import_errors
def initialize(type, import_errors)
message = "Import failed for `#{type}` with:\n"
import_errors.each do |action, action_errors|
message << " #{action.to_s.humanize} errors:\n"
Expand Down
13 changes: 7 additions & 6 deletions lib/chewy/fields/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class Base
attr_accessor :parent

def initialize(name, options = {})
@name, @options = name.to_sym, options.deep_symbolize_keys
@name = name.to_sym
@options = options.deep_symbolize_keys
@value = @options.delete(:value)
@children = []
end
Expand All @@ -15,7 +16,7 @@ def multi_field?
end

def object_field?
(children.present? && options[:type].blank?) || ['object', 'nested'].include?(options[:type].to_s)
(children.present? && options[:type].blank?) || %w(object nested).include?(options[:type].to_s)
end

def mappings_hash
Expand All @@ -24,22 +25,22 @@ def mappings_hash
} : {}
mapping.reverse_merge!(options)
mapping.reverse_merge!(type: (children.present? ? 'object' : 'string'))
{name => mapping}
{ name => mapping }
end

def compose(object, *parent_objects)
objects = ([object] + parent_objects.flatten).uniq

result = if value && value.is_a?(Proc)
if value.arity == 0
if value.arity.zero?
object.instance_exec(&value)
elsif value.arity < 0
value.call(*object)
else
value.call(*objects.first(value.arity))
end
elsif object.is_a?(Hash)
if object.has_key?(name)
if object.key?(name)
object[name]
else
object[name.to_s]
Expand All @@ -54,7 +55,7 @@ def compose(object, *parent_objects)
compose_children(result, *objects)
end if children.present? && !multi_field?

{name => result}
{ name => result }
end

private
Expand Down
8 changes: 4 additions & 4 deletions lib/chewy/fields/root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ def mappings_hash
mappings
end

def dynamic_template *args
def dynamic_template(*args)
options = args.extract_options!.deep_symbolize_keys
if args.first
template_name = :"template_#{dynamic_templates.count.next}"
template = {template_name => {mapping: options}}
template = { template_name => { mapping: options } }

template[template_name][:match_mapping_type] = args.second.to_s if args.second.present?

Expand All @@ -57,13 +57,13 @@ def dynamic_template *args

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

def compose_id(object)
if id
id.arity == 0 ? object.instance_exec(&id) : id.call(object)
id.arity.zero? ? object.instance_exec(&id) : id.call(object)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/chewy/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def self.#{type_class.type_name}
# UsersIndex.filters { name =~ 'ro' }.types(:admin, :manager)
# UsersIndex.types(:admin, :manager).filters { name =~ 'ro' } # the same as the first example
#
def self.types *args
def self.types(*args)
if args.present?
all.types(*args)
else
Expand Down Expand Up @@ -164,9 +164,9 @@ def self.journal?
types.any?(&:journal?)
end

def self.build_index_name *args
def self.build_index_name(*args)
options = args.extract_options!
[options[:prefix], args.first || index_name, options[:suffix]].reject(&:blank?).join(?_)
[options[:prefix], args.first || index_name, options[:suffix]].reject(&:blank?).join('_')
end

def self.settings_hash
Expand All @@ -175,7 +175,7 @@ def self.settings_hash

def self.mappings_hash
mappings = types.map(&:mappings_hash).inject(:merge)
mappings.present? ? {mappings: mappings} : {}
mappings.present? ? { mappings: mappings } : {}
end

def self.index_params
Expand Down
24 changes: 12 additions & 12 deletions lib/chewy/index/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def exists?
# Suffixed index names might be used for zero-downtime mapping change, for example.
# Description: (http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/).
#
def create *args
def create(*args)
create!(*args)
rescue Elasticsearch::Transport::Transport::Errors::BadRequest
false
Expand All @@ -52,13 +52,13 @@ def create *args
# Suffixed index names might be used for zero-downtime mapping change, for example.
# Description: (http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/).
#
def create! *args
def create!(*args)
options = args.extract_options!.reverse_merge!(alias: true)
name = build_index_name(suffix: args.first)

if Chewy::Runtime.version >= 1.1
body = index_params
body[:aliases] = {index_name => {}} if options[:alias] && name != index_name
body[:aliases] = { index_name => {} } if options[:alias] && name != index_name
result = client.indices.create(index: name, body: body)
else
result = client.indices.create(index: name, body: index_params)
Expand All @@ -77,7 +77,7 @@ def create! *args
#
# UsersIndex.delete '01-2014' # deletes `users_01-2014` index
#
def delete suffix = nil
def delete(suffix = nil)
result = client.indices.delete index: build_index_name(suffix: suffix)
Chewy.wait_for_status if result
result
Expand All @@ -96,7 +96,7 @@ def delete suffix = nil
#
# UsersIndex.delete '01-2014' # deletes `users_01-2014` index
#
def delete! suffix = nil
def delete!(suffix = nil)
# es-ruby >= 1.0.10 handles Elasticsearch::Transport::Transport::Errors::NotFound
# by itself, so it is raised here
delete(suffix) or raise Elasticsearch::Transport::Transport::Errors::NotFound
Expand All @@ -108,7 +108,7 @@ def delete! suffix = nil
# UsersIndex.purge # deletes and creates `users` index
# UsersIndex.purge '01-2014' # deletes `users` and `users_01-2014` indexes, creates `users_01-2014`
#
def purge suffix = nil
def purge(suffix = nil)
delete if suffix.present?
delete suffix
create suffix
Expand All @@ -121,7 +121,7 @@ def purge suffix = nil
# UsersIndex.purge! # deletes and creates `users` index
# UsersIndex.purge! '01-2014' # deletes `users` and `users_01-2014` indexes, creates `users_01-2014`
#
def purge! suffix = nil
def purge!(suffix = nil)
delete if suffix.present? && exists?
delete suffix
create! suffix
Expand Down Expand Up @@ -160,16 +160,16 @@ def #{method} options = {}
#
# UsersIndex.reset! Time.now.to_i, journal: true
#
def reset! suffix = nil, journal: false
def reset!(suffix = nil, journal: false)
if suffix.present? && (indexes = self.indexes).present?
create! suffix, alias: false
result = import suffix: suffix, journal: journal
client.indices.update_aliases body: {actions: [
client.indices.update_aliases body: { actions: [
*indexes.map do |index|
{remove: {index: index, alias: index_name}}
{ remove: { index: index, alias: index_name } }
end,
{add: {index: build_index_name(suffix: suffix), alias: index_name}}
]}
{ add: { index: build_index_name(suffix: suffix), alias: index_name } }
] }
client.indices.delete index: indexes if indexes.present?
result
else
Expand Down
5 changes: 2 additions & 3 deletions lib/chewy/index/settings.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Chewy
class Index

# Stores ElasticSearch index settings and resolves `analysis`
# hash. At first, you need to store some analyzers or other
# analysis options to the corresponding repository:
Expand Down Expand Up @@ -37,7 +36,7 @@ def to_hash
.deep_merge((settings[:index] || {}).deep_symbolize_keys)
end

settings.present? ? {settings: settings} : {}
settings.present? ? { settings: settings } : {}
end

private
Expand Down Expand Up @@ -67,7 +66,7 @@ def resolve(params, repository)
else
name_or_hash = name_or_hash.to_sym
resolved = repository[name_or_hash]
resolved ? {name_or_hash => resolved} : {}
resolved ? { name_or_hash => resolved } : {}
end
result.merge!(options)
end
Expand Down
Loading

0 comments on commit bf8a74b

Please sign in to comment.