From a65ba3ff3803c58ce18ea5f960ac96e5e53bddcd Mon Sep 17 00:00:00 2001 From: Gerard Caulfield Date: Wed, 9 Nov 2016 17:04:02 +1100 Subject: [PATCH] Use assignments consistent with our style guide This is so that people do not get warnings about problems in existing code when we turn on Hound style checks. --- If git-blame brought you here you may want to read this, the problem is with git-blame, not this change. Try running these two lines just once: ``` git config --global alias.praise 'log -p -M --follow --stat --' git config --global alias.praise-line 'log -p -M --pretty=format:"%h (%an %ai)" -L' ``` Now in future you can use `git praise ` or if you want to see the evolution of a specific line or range of lines `git praise-line ::` Some examples you should try: ``` git praise lib/awesome_print/version.rb git praise-line 8:8:lib/awesome_print/version.rb ``` Inspiration for these aliases: http://blog.andrewray.me/a-better-git-blame/ --- lib/awesome_print/ext/mongo_mapper.rb | 10 ++++----- lib/awesome_print/formatter.rb | 2 +- .../formatters/base_formatter.rb | 13 ++++++----- .../formatters/class_formatter.rb | 3 ++- .../formatters/hash_formatter.rb | 9 +++++--- .../formatters/object_formatter.rb | 3 ++- .../formatters/struct_formatter.rb | 3 ++- spec/colors_spec.rb | 12 ++++++---- spec/ext/mongo_mapper_spec.rb | 14 ++++++------ spec/objects_spec.rb | 22 ++++++++++++++----- 10 files changed, 56 insertions(+), 35 deletions(-) diff --git a/lib/awesome_print/ext/mongo_mapper.rb b/lib/awesome_print/ext/mongo_mapper.rb index 42d8edb..840e3d7 100644 --- a/lib/awesome_print/ext/mongo_mapper.rb +++ b/lib/awesome_print/ext/mongo_mapper.rb @@ -73,11 +73,11 @@ module AwesomePrint # Add in associations if @options[:mongo_mapper][:show_associations] object.associations.each do |name, assoc| - if @options[:mongo_mapper][:inline_embedded] and assoc.embeddable? - data[name.to_s] = object.send(name) - else - data[name.to_s] = assoc - end + data[name.to_s] = if @options[:mongo_mapper][:inline_embedded] and assoc.embeddable? + object.send(name) + else + assoc + end end end diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index 2d87b94..0d59304 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -44,7 +44,7 @@ module AwesomePrint def awesome_self(object, type) if @options[:raw] && object.instance_variables.any? awesome_object(object) - elsif hash = convert_to_hash(object) + elsif (hash = convert_to_hash(object)) awesome_hash(hash) else awesome_simple(object.inspect.to_s, type, @inspector) diff --git a/lib/awesome_print/formatters/base_formatter.rb b/lib/awesome_print/formatters/base_formatter.rb index 05353f8..4483a4b 100644 --- a/lib/awesome_print/formatters/base_formatter.rb +++ b/lib/awesome_print/formatters/base_formatter.rb @@ -52,11 +52,11 @@ module AwesomePrint # Add the proper elements to the temp array and format the separator. temp = data[0, head] + [nil] + data[-tail, tail] - if is_hash - temp[head] = "#{indent}#{data[head].strip} .. #{data[data.length - tail - 1].strip}" - else - temp[head] = "#{indent}[#{head.to_s.rjust(width)}] .. [#{data.length - tail - 1}]" - end + temp[head] = if is_hash + "#{indent}#{data[head].strip} .. #{data[data.length - tail - 1].strip}" + else + "#{indent}[#{head.to_s.rjust(width)}] .. [#{data.length - tail - 1}]" + end temp end @@ -90,7 +90,8 @@ module AwesomePrint # # # if method.to_s =~ /(Unbound)*Method: (.*)[#\.]/ - unbound, klass = $1 && '(unbound)', $2 + unbound = $1 && '(unbound)' + klass = $2 if klass && klass =~ /(\(\w+:\s.*?\))/ # Is this ActiveRecord-style class? klass.sub!($1, '') # Yes, strip the fields leaving class name only. end diff --git a/lib/awesome_print/formatters/class_formatter.rb b/lib/awesome_print/formatters/class_formatter.rb index 8cc1089..d5082ea 100644 --- a/lib/awesome_print/formatters/class_formatter.rb +++ b/lib/awesome_print/formatters/class_formatter.rb @@ -13,7 +13,8 @@ module AwesomePrint end def format - if superclass = klass.superclass # <-- Assign and test if nil. + superclass = klass.superclass + if superclass colorize("#{klass.inspect} < #{superclass}", :class) else colorize(klass.inspect, :class) diff --git a/lib/awesome_print/formatters/hash_formatter.rb b/lib/awesome_print/formatters/hash_formatter.rb index cf398c8..79fa200 100644 --- a/lib/awesome_print/formatters/hash_formatter.rb +++ b/lib/awesome_print/formatters/hash_formatter.rb @@ -60,11 +60,14 @@ module AwesomePrint end def plain_single_line - plain, multiline = options[:plain], options[:multiline] - options[:plain], options[:multiline] = true, false + plain = options[:plain] + multiline = options[:multiline] + options[:plain] = true + options[:multiline] = false yield ensure - options[:plain], options[:multiline] = plain, multiline + options[:plain] = plain + options[:multiline] = multiline end end end diff --git a/lib/awesome_print/formatters/object_formatter.rb b/lib/awesome_print/formatters/object_formatter.rb index 3ab3429..655a8e6 100644 --- a/lib/awesome_print/formatters/object_formatter.rb +++ b/lib/awesome_print/formatters/object_formatter.rb @@ -64,7 +64,8 @@ module AwesomePrint end def left_aligned - current, options[:indent] = options[:indent], 0 + current = options[:indent] + options[:indent] = 0 yield ensure options[:indent] = current diff --git a/lib/awesome_print/formatters/struct_formatter.rb b/lib/awesome_print/formatters/struct_formatter.rb index 86cee9a..18bebaa 100644 --- a/lib/awesome_print/formatters/struct_formatter.rb +++ b/lib/awesome_print/formatters/struct_formatter.rb @@ -60,7 +60,8 @@ module AwesomePrint end def left_aligned - current, options[:indent] = options[:indent], 0 + current = options[:indent] + options[:indent] = 0 yield ensure options[:indent] = current diff --git a/spec/colors_spec.rb b/spec/colors_spec.rb index c0c51ae..adccc6b 100644 --- a/spec/colors_spec.rb +++ b/spec/colors_spec.rb @@ -32,7 +32,8 @@ RSpec.describe 'AwesomePrint' do it "colorizes processes with ENV['ANSICON'] by default" do begin stub_tty! - term, ENV['ANSICON'] = ENV['ANSICON'], '1' + term = ENV['ANSICON'] + ENV['ANSICON'] = '1' expect(@arr.ai(multiline: false)).to eq(COLORIZED) ensure ENV['ANSICON'] = term @@ -42,7 +43,8 @@ RSpec.describe 'AwesomePrint' do it 'does not colorize tty processes running in dumb terminals by default' do begin stub_tty! - term, ENV['TERM'] = ENV['TERM'], 'dumb' + term = ENV['TERM'] + ENV['TERM'] = 'dumb' expect(@arr.ai(multiline: false)).to eq(PLAIN) ensure ENV['TERM'] = term @@ -72,7 +74,8 @@ RSpec.describe 'AwesomePrint' do it "colorizes processes with ENV['ANSICON'] set to 0" do begin stub_tty! - term, ENV['ANSICON'] = ENV['ANSICON'], '1' + term = ENV['ANSICON'] + ENV['ANSICON'] = '1' expect(@arr.ai(multiline: false)).to eq(COLORIZED) ensure ENV['ANSICON'] = term @@ -82,7 +85,8 @@ RSpec.describe 'AwesomePrint' do it 'colorizes dumb terminals' do begin stub_tty! - term, ENV['TERM'] = ENV['TERM'], 'dumb' + term = ENV['TERM'] + ENV['TERM'] = 'dumb' expect(@arr.ai(multiline: false)).to eq(COLORIZED) ensure ENV['TERM'] = term diff --git a/spec/ext/mongo_mapper_spec.rb b/spec/ext/mongo_mapper_spec.rb index b9da6f0..55cb4eb 100644 --- a/spec/ext/mongo_mapper_spec.rb +++ b/spec/ext/mongo_mapper_spec.rb @@ -32,8 +32,8 @@ RSpec.describe 'AwesomePrint/MongoMapper', skip: -> { !ExtVerifier.has_mongo_map out.gsub!(/#\/, 'AWESOME_PRINT_PROC_STUB') out.gsub!(/BSON::ObjectId\('[\da-f]+?'\)/, "BSON::ObjectId('123456789')") - if MongoMapper::Version >= '0.13' - str = <<-EOS.strip + str = if MongoMapper::Version >= '0.13' + <<-EOS.strip # { !ExtVerifier.has_mongo_map "last_name" => nil } > - EOS - else - str = <<-EOS.strip + EOS + else + <<-EOS.strip # { !ExtVerifier.has_mongo_map attr_reader :first_name_before_type_cast = "Al", attr_reader :last_name_before_type_cast = "Capone" > - EOS - end + EOS + end expect(out).to be_similar_to(str) end diff --git a/spec/objects_spec.rb b/spec/objects_spec.rb index fd5e534..3aaef90 100644 --- a/spec/objects_spec.rb +++ b/spec/objects_spec.rb @@ -13,7 +13,9 @@ RSpec.describe 'Objects' do attr_accessor :dabra def initialize - @abra, @ca, @dabra = 1, 2, 3 + @abra = 1 + @ca = 2 + @dabra = 3 end end @@ -33,7 +35,9 @@ EOS it 'instance variables' do class Hello def initialize - @abra, @ca, @dabra = 1, 2, 3 + @abra = 1 + @ca = 2 + @dabra = 3 end end @@ -57,8 +61,12 @@ EOS attr_accessor :dabra def initialize - @abra, @ca, @dabra = 1, 2, 3 - @scooby, @dooby, @doo = 3, 2, 1 + @abra = 1 + @ca = 2 + @dabra = 3 + @scooby = 3 + @dooby = 2 + @doo = 1 end end @@ -84,7 +92,8 @@ EOS attr_writer :ca def initialize - @abra, @ca = 1, 2 + @abra = 1 + @ca = 2 @dabra = 3 end end @@ -108,7 +117,8 @@ EOS attr_writer :ca def initialize - @abra, @ca = 1, 2 + @abra = 1 + @ca = 2 @dabra = 3 end end