From fc658f3e89683b8ff38a3109408612c9c28b0a28 Mon Sep 17 00:00:00 2001 From: Sean Earle Date: Wed, 16 May 2018 16:30:34 +0930 Subject: [PATCH 1/3] Setup rubocop --- .rubocop.yml | 21 +++++++++++++++++++++ ruby_core_extensions.gemspec | 1 + 2 files changed, 22 insertions(+) create mode 100644 .rubocop.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..c58175d --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,21 @@ +inherit_gem: + rubocop-rails: + - config/rails.yml + +AllCops: + TargetRubyVersion: 2.4 + +Style/FrozenStringLiteralComment: + Enabled: false + +Style/StringLiterals: + Enabled: false + +Layout/IndentationWidth: + IgnoredPatterns: ['^\s*private$'] + +Style/BracesAroundHashParameters: + Enabled: false + +Metrics/LineLength: + Max: 100 diff --git a/ruby_core_extensions.gemspec b/ruby_core_extensions.gemspec index 7686d6c..5b65210 100644 --- a/ruby_core_extensions.gemspec +++ b/ruby_core_extensions.gemspec @@ -29,4 +29,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'simplecov-rcov' spec.add_development_dependency 'coveralls' spec.add_development_dependency 'travis' + spec.add_development_dependency 'rubocop-rails' end From 65f3c3d2268937fa13d1771a317e8a1191835213 Mon Sep 17 00:00:00 2001 From: Sean Earle Date: Wed, 16 May 2018 16:30:46 +0930 Subject: [PATCH 2/3] Follow the rubocop warnings --- Rakefile | 2 +- gemfiles/rails3.gemfile | 2 +- gemfiles/rails4.gemfile | 2 +- lib/ruby_core_extensions/array.rb | 3 +- lib/ruby_core_extensions/class.rb | 3 - lib/ruby_core_extensions/compact/array.rb | 3 +- lib/ruby_core_extensions/compact/hash.rb | 7 +- lib/ruby_core_extensions/enumerable.rb | 3 - lib/ruby_core_extensions/file.rb | 1 - lib/ruby_core_extensions/hash.rb | 10 +-- lib/ruby_core_extensions/kernel.rb | 2 - lib/ruby_core_extensions/numeric.rb | 2 - lib/ruby_core_extensions/object.rb | 12 ++- lib/ruby_core_extensions/recursive/array.rb | 86 +++++++++---------- lib/ruby_core_extensions/recursive/date.rb | 3 - .../recursive/date_time.rb | 3 - lib/ruby_core_extensions/recursive/fixnum.rb | 2 - lib/ruby_core_extensions/recursive/hash.rb | 31 +++---- lib/ruby_core_extensions/recursive/object.rb | 6 +- lib/ruby_core_extensions/recursive/time.rb | 3 - lib/ruby_core_extensions/string.rb | 17 ++-- ruby_core_extensions.gemspec | 1 + spec/array_spec.rb | 26 +++--- spec/compact_spec.rb | 26 +++--- spec/enumerable_spec.rb | 22 +++-- spec/filename_spec.rb | 1 - spec/hash_spec.rb | 71 ++++++++------- spec/object_spec.rb | 9 +- spec/string_spec.rb | 4 +- 29 files changed, 175 insertions(+), 188 deletions(-) diff --git a/Rakefile b/Rakefile index 9f429c0..ff5018d 100644 --- a/Rakefile +++ b/Rakefile @@ -1,7 +1,7 @@ require "bundler/gem_tasks" desc 'Default: run specs.' -task :default => :spec +task default: :spec require 'rspec/core/rake_task' diff --git a/gemfiles/rails3.gemfile b/gemfiles/rails3.gemfile index b1ae326..5af4c96 100644 --- a/gemfiles/rails3.gemfile +++ b/gemfiles/rails3.gemfile @@ -1,5 +1,5 @@ source 'https://rubygems.org' -gemspec :path => '../' +gemspec path: '../' group :development, :test do gem 'rake' diff --git a/gemfiles/rails4.gemfile b/gemfiles/rails4.gemfile index 92ef7f9..7c0c2a9 100644 --- a/gemfiles/rails4.gemfile +++ b/gemfiles/rails4.gemfile @@ -1,5 +1,5 @@ source 'https://rubygems.org' -gemspec :path => '../' +gemspec path: '../' group :development, :test do gem 'rake' diff --git a/lib/ruby_core_extensions/array.rb b/lib/ruby_core_extensions/array.rb index 7efb777..9ce4966 100644 --- a/lib/ruby_core_extensions/array.rb +++ b/lib/ruby_core_extensions/array.rb @@ -1,5 +1,4 @@ class Array - def to_param self.collect { |element| element.respond_to?(:to_param) ? element.to_param : element } end @@ -31,7 +30,7 @@ def hash_by_id(method = nil, &block) end def intersects?(other) - self.any?{|i| other.include?(i)} + self.any? { |i| other.include?(i) } end # Same effect as Array.wrap(object).first diff --git a/lib/ruby_core_extensions/class.rb b/lib/ruby_core_extensions/class.rb index a841437..a26ce01 100644 --- a/lib/ruby_core_extensions/class.rb +++ b/lib/ruby_core_extensions/class.rb @@ -1,8 +1,5 @@ class Class - def downcase_symbol self.to_s.downcase.to_sym end - end - diff --git a/lib/ruby_core_extensions/compact/array.rb b/lib/ruby_core_extensions/compact/array.rb index 68ee9b0..0d7cd0b 100644 --- a/lib/ruby_core_extensions/compact/array.rb +++ b/lib/ruby_core_extensions/compact/array.rb @@ -1,6 +1,6 @@ class Array def compact_blank! - delete_if{|v| v.blank?} + delete_if { |v| v.blank? } end def recursive_compact_blank! @@ -17,4 +17,3 @@ def recursive_compact_blank! end end end - diff --git a/lib/ruby_core_extensions/compact/hash.rb b/lib/ruby_core_extensions/compact/hash.rb index e9d1646..8207cce 100644 --- a/lib/ruby_core_extensions/compact/hash.rb +++ b/lib/ruby_core_extensions/compact/hash.rb @@ -6,7 +6,7 @@ def compact # Remove nil values - !desctructively! def compact! - delete_if{|k,v| v.nil?} + delete_if { |k, v| v.nil? } end def compact_blank @@ -14,11 +14,11 @@ def compact_blank end def compact_blank! - delete_if{|k,v| v.blank?} + delete_if { |k, v| v.blank? } end def recursive_compact_blank! - delete_if do |k,v| + delete_if do |k, v| if v.is_a?(Hash) v.recursive_compact_blank! v.recursive_blank? @@ -31,4 +31,3 @@ def recursive_compact_blank! end end end - diff --git a/lib/ruby_core_extensions/enumerable.rb b/lib/ruby_core_extensions/enumerable.rb index f8cd2d6..d57bba5 100644 --- a/lib/ruby_core_extensions/enumerable.rb +++ b/lib/ruby_core_extensions/enumerable.rb @@ -1,5 +1,4 @@ module Enumerable - def map_methods(*methods) map do |object| methods.inject({}) do |h, method| @@ -34,6 +33,4 @@ def with_object(obj, &block) obj end end - end - diff --git a/lib/ruby_core_extensions/file.rb b/lib/ruby_core_extensions/file.rb index a967343..66131b7 100644 --- a/lib/ruby_core_extensions/file.rb +++ b/lib/ruby_core_extensions/file.rb @@ -9,4 +9,3 @@ def self.safe_name(name) gsub(/\-+/, '-') # limit - character to once end end - diff --git a/lib/ruby_core_extensions/hash.rb b/lib/ruby_core_extensions/hash.rb index 79d6040..0643b4b 100644 --- a/lib/ruby_core_extensions/hash.rb +++ b/lib/ruby_core_extensions/hash.rb @@ -1,7 +1,6 @@ class Hash - unless self.method_defined?(:extract!) - #Imported from Rails 3 + # Imported from Rails 3 def extract!(*keys) result = {} keys.each { |key| result[key] = delete(key) } @@ -12,23 +11,22 @@ def extract!(*keys) def map_key_value(key_method, value_method = nil) value_method ||= key_method - each.with_object({}) do |(k,v), new_hash| + each.with_object({}) do |(k, v), new_hash| new_hash[k.send(key_method)] = v.send(value_method) end end def map_key(method) - each.with_object({}) do |(k,v), new_hash| + each.with_object({}) do |(k, v), new_hash| new_hash[k.send(method)] = v end end def map_value(method) - each.with_object({}) do |(k,v), new_hash| + each.with_object({}) do |(k, v), new_hash| new_hash[k] = v.send(method) end end - end diff --git a/lib/ruby_core_extensions/kernel.rb b/lib/ruby_core_extensions/kernel.rb index a5127a5..110ca97 100644 --- a/lib/ruby_core_extensions/kernel.rb +++ b/lib/ruby_core_extensions/kernel.rb @@ -1,5 +1,4 @@ module Kernel - def require_dirs(dirs) Array.wrap(dirs).each do |load_path| Dir.glob("#{load_path}/**/*.rb").each do |file| @@ -7,5 +6,4 @@ def require_dirs(dirs) end end end - end diff --git a/lib/ruby_core_extensions/numeric.rb b/lib/ruby_core_extensions/numeric.rb index 0299de5..0b260c0 100644 --- a/lib/ruby_core_extensions/numeric.rb +++ b/lib/ruby_core_extensions/numeric.rb @@ -1,7 +1,5 @@ class Numeric - def to_bool !zero? end - end diff --git a/lib/ruby_core_extensions/object.rb b/lib/ruby_core_extensions/object.rb index 53f77fd..ec13232 100644 --- a/lib/ruby_core_extensions/object.rb +++ b/lib/ruby_core_extensions/object.rb @@ -3,16 +3,16 @@ class Object def sounds_like?(other) self.phonetic_code == other.phonetic_code end - + # Convert this object into a string, then convert that to phonetic code def phonetic_code self.to_s.phonetic_code end - + def to_long_s to_s end - + def virtual_belongs_to(*associations) options = associations.extract_options! @@ -62,11 +62,9 @@ def #{normal_name}?(*args) end EVAL end - - + + def to_bool self.to_s.to_bool end - end - diff --git a/lib/ruby_core_extensions/recursive/array.rb b/lib/ruby_core_extensions/recursive/array.rb index 4e81c6b..fa2a179 100644 --- a/lib/ruby_core_extensions/recursive/array.rb +++ b/lib/ruby_core_extensions/recursive/array.rb @@ -1,45 +1,43 @@ - class Array - - def convert - self - end + class Array + def convert + self + end - def convert_keys_recursively(&converter) - map { |v| v.convert_keys_recursively(&converter) } - end - - def convert_values_recursively(&converter) - map { |v| v.convert_values_recursively(&converter) } - end - - def symbolize_keys_recursively - map(&:symbolize_keys_recursively) - end - - def stringify_values_recursively - map(&:stringify_values_recursively) - end - - def make_indifferent_access_recursively - map(&:make_indifferent_access_recursively) - end - - def recursive_blank? - each do |v| - if v.respond_to?(:recursive_blank?) - return false unless v.recursive_blank? - else - return false unless v.blank? - end - end - true - end - - def recursively(&block) - each do |element| - block.call(element) - element.recursively(&block) if element.respond_to?(:recursively) - end - end - -end + def convert_keys_recursively(&converter) + map { |v| v.convert_keys_recursively(&converter) } + end + + def convert_values_recursively(&converter) + map { |v| v.convert_values_recursively(&converter) } + end + + def symbolize_keys_recursively + map(&:symbolize_keys_recursively) + end + + def stringify_values_recursively + map(&:stringify_values_recursively) + end + + def make_indifferent_access_recursively + map(&:make_indifferent_access_recursively) + end + + def recursive_blank? + each do |v| + if v.respond_to?(:recursive_blank?) + return false unless v.recursive_blank? + else + return false unless v.blank? + end + end + true + end + + def recursively(&block) + each do |element| + block.call(element) + element.recursively(&block) if element.respond_to?(:recursively) + end + end + end diff --git a/lib/ruby_core_extensions/recursive/date.rb b/lib/ruby_core_extensions/recursive/date.rb index cb8233b..e472b69 100644 --- a/lib/ruby_core_extensions/recursive/date.rb +++ b/lib/ruby_core_extensions/recursive/date.rb @@ -1,8 +1,5 @@ class Date - def stringify_values_recursively self.to_s end - end - diff --git a/lib/ruby_core_extensions/recursive/date_time.rb b/lib/ruby_core_extensions/recursive/date_time.rb index 1f6b58a..9ddde07 100644 --- a/lib/ruby_core_extensions/recursive/date_time.rb +++ b/lib/ruby_core_extensions/recursive/date_time.rb @@ -1,8 +1,5 @@ class DateTime - def stringify_values_recursively self.to_s end - end - diff --git a/lib/ruby_core_extensions/recursive/fixnum.rb b/lib/ruby_core_extensions/recursive/fixnum.rb index 6efa5f5..abea317 100644 --- a/lib/ruby_core_extensions/recursive/fixnum.rb +++ b/lib/ruby_core_extensions/recursive/fixnum.rb @@ -1,7 +1,5 @@ class Fixnum - def stringify_values_recursively to_s end - end diff --git a/lib/ruby_core_extensions/recursive/hash.rb b/lib/ruby_core_extensions/recursive/hash.rb index 49244aa..e9d7529 100644 --- a/lib/ruby_core_extensions/recursive/hash.rb +++ b/lib/ruby_core_extensions/recursive/hash.rb @@ -1,5 +1,4 @@ class Hash - def recursive_blank? each do |k, v| if v.respond_to?(:recursive_blank?) @@ -10,18 +9,18 @@ def recursive_blank? end true end - + def convert self end - + def convert_keys(&converter) inject({}) do |hash, (key, value)| hash[converter.call(key)] = value hash end end - + def convert_values(*keys, &converter) inject(clone) do |hash, (key, value)| hash[key] = value.convert(&converter) if keys.blank? || keys.include?(key) @@ -33,54 +32,52 @@ def convert_keys_recursively(&converter) Hash[map do |key, value| k = converter.call(key) v = value.convert_keys_recursively(&converter) - [k,v] + [k, v] end] end - + def convert_values_recursively(&converter) inject({}) do |hash, (key, value)| hash[key] = value.convert_values_recursively(&converter) hash end end - + def symbolize_keys_recursively Hash[map do |key, value| k = key.is_a?(String) ? key.to_sym : key v = value.symbolize_keys_recursively - [k,v] + [k, v] end] end - + def stringify_values_recursively inject({}) do |options, (key, value)| options[key] = value.stringify_values_recursively options end end - + def make_indifferent_access_recursively HashWithIndifferentAccess.new(inject({}) do |options, (key, value)| options[key] = value.make_indifferent_access_recursively options end) end - + def deep_dup duplicate = self.dup - duplicate.each_pair do |k,v| + duplicate.each_pair do |k, v| tv = duplicate[k] duplicate[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_dup : v end duplicate end - + def recursively(&block) - each do |key,value| - block.call(key,value) + each do |key, value| + block.call(key, value) value.recursively(&block) if value.respond_to?(:recursively) end end - end - diff --git a/lib/ruby_core_extensions/recursive/object.rb b/lib/ruby_core_extensions/recursive/object.rb index 6e8c4ab..186ce31 100644 --- a/lib/ruby_core_extensions/recursive/object.rb +++ b/lib/ruby_core_extensions/recursive/object.rb @@ -2,14 +2,14 @@ class Object def convert(&converter) converter.call(self) end - + def return_self self end - + alias_method :convert_values_recursively, :convert alias_method :convert_recursively, :convert - + alias_method :convert_keys_recursively, :return_self alias_method :symbolize_keys_recursively, :return_self alias_method :stringify_values_recursively, :return_self diff --git a/lib/ruby_core_extensions/recursive/time.rb b/lib/ruby_core_extensions/recursive/time.rb index 4a0edbb..522447e 100644 --- a/lib/ruby_core_extensions/recursive/time.rb +++ b/lib/ruby_core_extensions/recursive/time.rb @@ -1,8 +1,5 @@ class Time - def stringify_values_recursively self.to_s end - end - diff --git a/lib/ruby_core_extensions/string.rb b/lib/ruby_core_extensions/string.rb index 6ca848e..bd6c33f 100644 --- a/lib/ruby_core_extensions/string.rb +++ b/lib/ruby_core_extensions/string.rb @@ -1,8 +1,8 @@ class String def proper_underscore - self.titleize.gsub(" ","").underscore + self.titleize.gsub(" ", "").underscore end - + # Generate a phonetic code - which is the same for similar sounding names def phonetic_code # Currently using 'metaphone' which is more accurate than soundex @@ -11,7 +11,7 @@ def phonetic_code # Solr requires numbers and letters to be separated def separate_numbers_and_letters - gsub(/[a-z][0-9]|[0-9][a-z]/i){ |s| s[0].chr + ' ' + s[1].chr } + gsub(/[a-z][0-9]|[0-9][a-z]/i) { |s| s[0].chr + ' ' + s[1].chr } end # convert newlines to breaks @@ -42,13 +42,13 @@ def squash(limit) words = split(' ') # Return first letter of words - return words.first(limit).map{|w| w.chars.first}.join if (words.size * 2 - 1) >= limit + return words.first(limit).map { |w| w.chars.first }.join if (words.size * 2 - 1) >= limit spaces = words.size - 1 word_char_min = (limit - spaces) / words.size word_char_max = word_char_min + 1 - words = words.map{|word| word[0..(word_char_max - 1)]} + words = words.map { |word| word[0..(word_char_max - 1)] } words.reverse.each.with_index do |word, index| letters_to_remove = words.join(' ').size - limit @@ -60,19 +60,18 @@ def squash(limit) end # Replace word - words[words.size - index - 1] = word[0..(letters_to_keep - 1)] + words[words.size - index - 1] = word[0..(letters_to_keep - 1)] break if last_case end words.join(' ') end - - + + def to_bool return true if self == true || self =~ (/(true|t|yes|y|1)$/i) return false if self == false || self.blank? || self =~ (/(false|f|no|n|0)$/i) raise ArgumentError.new("invalid value for Boolean: \"#{self}\"") end end - diff --git a/ruby_core_extensions.gemspec b/ruby_core_extensions.gemspec index 5b65210..842b05b 100644 --- a/ruby_core_extensions.gemspec +++ b/ruby_core_extensions.gemspec @@ -1,4 +1,5 @@ # coding: utf-8 + lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'ruby_core_extensions/version' diff --git a/spec/array_spec.rb b/spec/array_spec.rb index 568a4d9..15537c0 100644 --- a/spec/array_spec.rb +++ b/spec/array_spec.rb @@ -4,7 +4,9 @@ it "should allow converting all values to strings recursively" do @now = Time.now - expect([1, 2, @now, [3, 4]].stringify_values_recursively).to eq ['1', '2', @now.to_s, ['3', '4']] + expect([1, 2, @now, [3, 4]].stringify_values_recursively).to eq( + ['1', '2', @now.to_s, ['3', '4']] + ) end it "should allow removing all blank values" do @@ -14,30 +16,34 @@ end it "should allow removing all blank values recursively" do - a = [1, 2, [" Kan", {}], nil, {:a => "", :b => {}}, ["garoos", " "]] + a = [1, 2, [" Kan", {}], nil, { a: "", b: {} }, ["garoos", " "]] a.recursive_compact_blank! expect(a.join).to eq "12 Kangaroos" end it "should allow verifying if all elements are blank recursively" do - expect(['',nil,[nil,['']]]).to be_recursive_blank - expect(['',nil,[nil,['',1]]]).to_not be_recursive_blank + expect(['', nil, [nil, ['']]]).to be_recursive_blank + expect(['', nil, [nil, ['', 1]]]).to_not be_recursive_blank end it "should allow converting to hash given a key" do - expect([1,2,3].hash_by(:ordinalize)).to eq({'1st' => 1, "2nd" => 2, "3rd" => 3}) - expect([1,2,3].hash_by(:ordinalize, :to_s)).to eq({'1st' => '1', "2nd" => '2', "3rd" => '3'}) - expect([1,2,3].hash_by(:ordinalize){ |v| v + 1 }).to eq({'1st' => 2, "2nd" => 3, "3rd" => 4}) - expect([1,2,3].hash_by{|k| k * 2}).to eq({2 => 1, 4 => 2, 6 => 3}) + expect([1, 2, 3].hash_by(:ordinalize)).to eq({ '1st' => 1, "2nd" => 2, "3rd" => 3 }) + expect([1, 2, 3].hash_by(:ordinalize, :to_s)).to eq( + { '1st' => '1', "2nd" => '2', "3rd" => '3' } + ) + expect([1, 2, 3].hash_by(:ordinalize) { |v| v + 1 }).to eq( + { '1st' => 2, "2nd" => 3, "3rd" => 4 } + ) + expect([1, 2, 3].hash_by { |k| k * 2 }).to eq({ 2 => 1, 4 => 2, 6 => 3 }) end it "should allow executing blocks recursively" do - array = [1,[2,3],[4,[5,6],7,[8]],9,[[10]]] + array = [1, [2, 3], [4, [5, 6], 7, [8]], 9, [[10]]] result = [] array.recursively do |e| result << e unless e.is_a?(Array) end - expect(result).to eq [1,2,3,4,5,6,7,8,9,10] + expect(result).to eq [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] end it '#intersects?' do diff --git a/spec/compact_spec.rb b/spec/compact_spec.rb index c7f633f..a8c6490 100644 --- a/spec/compact_spec.rb +++ b/spec/compact_spec.rb @@ -6,11 +6,11 @@ end it 'should recursive compact blank' do - a = [1, nil, {:a => nil, :b => 2}] + a = [1, nil, { a: nil, b: 2 }] a.recursive_compact_blank! - expect(a).to eq [1, {:b => 2}] + expect(a).to eq [1, { b: 2 }] - b = [1, nil, {:a => nil}] + b = [1, nil, { a: nil }] b.recursive_compact_blank! expect(b).to eq [1] end @@ -18,32 +18,32 @@ describe Hash, 'compact' do it 'should compact' do - expect({:a => 1, :b => nil}.compact).to eq({:a => 1}) + expect({ a: 1, b: nil }.compact).to eq({ a: 1 }) end it 'should compact!' do - a = {:a => 1, :b => nil} + a = { a: 1, b: nil } a.compact! - expect(a).to eq({:a => 1}) + expect(a).to eq({ a: 1 }) end it 'should compact blank' do - expect({:a => 1, :b => ''}.compact_blank).to eq({:a => 1}) + expect({ a: 1, b: '' }.compact_blank).to eq({ a: 1 }) end it 'should compact blank!' do - a = {:a => 1, :b => ''} + a = { a: 1, b: '' } a.compact_blank! - expect(a).to eq({:a => 1}) + expect(a).to eq({ a: 1 }) end it 'should recursive compact blank!' do - a = {:a => 1, :b => {:c => 1, :d => ''}} + a = { a: 1, b: { c: 1, d: '' } } a.recursive_compact_blank! - expect(a).to eq({:a => 1, :b => {:c => 1}}) + expect(a).to eq({ a: 1, b: { c: 1 } }) - a = {:a => 1, :b => {:c => [], :d => ''}} + a = { a: 1, b: { c: [], d: '' } } a.recursive_compact_blank! - expect(a).to eq({:a => 1}) + expect(a).to eq({ a: 1 }) end end diff --git a/spec/enumerable_spec.rb b/spec/enumerable_spec.rb index d1daf99..52b859b 100644 --- a/spec/enumerable_spec.rb +++ b/spec/enumerable_spec.rb @@ -1,24 +1,28 @@ require 'spec_helper' describe Enumerable do - - it 'should allow mapping elements of the collection to hashes associating method names to the returned values for each method' do - expect([1,2,3].map_methods(:to_s,:to_f)).to eq [{:to_s => '1', :to_f => 1.0}, {:to_s => '2', :to_f => 2.0}, {:to_s => '3', :to_f => 3.0}] + + it 'allow mapping elements to hashes with method names of the returned values for each method' do + expect([1, 2, 3].map_methods(:to_s, :to_f)).to eq( + [{ to_s: '1', to_f: 1.0 }, { to_s: '2', to_f: 2.0 }, { to_s: '3', to_f: 3.0 }] + ) end context 'when detecting and returning the block value' do - it { expect([1,2,3].detect_and_return { |number| number.even? && number * number }).to eq 4 } - it { expect([1,3,5].detect_and_return { |number| number.even? && number * number }).to be nil } + it { expect([1, 2, 3].detect_and_return { |number| number.even? && number * number }).to eq 4 } + it { expect([1, 3, 5].detect_and_return { |number| + number.even? && number * number }).to be nil + } end it 'should allow selecting by attribute' do - one = double(:name => 'one', :type => 'odd') - two = double(:name => 'two', :type => 'even') - thr = double(:name => 'thr', :type => 'odd') + one = double(name: 'one', type: 'odd') + two = double(name: 'two', type: 'even') + thr = double(name: 'thr', type: 'odd') expect([one, two, thr].select_by_attr(:type, 'odd')).to eq [one, thr] expect([one, two, thr].select_by_attr(:type, 'even')).to eq [two] end - + end diff --git a/spec/filename_spec.rb b/spec/filename_spec.rb index f828a5a..e2baee9 100644 --- a/spec/filename_spec.rb +++ b/spec/filename_spec.rb @@ -14,4 +14,3 @@ def safe(from) File.safe_name(from) end end - diff --git a/spec/hash_spec.rb b/spec/hash_spec.rb index cb48eca..cf4134d 100644 --- a/spec/hash_spec.rb +++ b/spec/hash_spec.rb @@ -3,14 +3,21 @@ describe Hash do before do - @sub_array1 = [3, BigDecimal('4'), Date.new(2000, 1, 1), DateTime.new(2000, 1, 1, 0, 0, 0), {:f => 5}] - @sub_array2 = [3, BigDecimal('4'), Date.new(2000, 1, 1), DateTime.new(2000, 1, 1, 0, 0, 0), {'f' => 5}] - @hash1 = {:a => 1, :b => {:c => 2}, :d => 'test', :e => @sub_array1} - @hash2 = {'a' => 1, 'b' => {'c' => 2}, :d => 'test', 'e' => @sub_array2} + @sub_array1 = [3, BigDecimal('4'), Date.new(2000, 1, 1), + DateTime.new(2000, 1, 1, 0, 0, 0), { f: 5 }] + @sub_array2 = [3, BigDecimal('4'), Date.new(2000, 1, 1), + DateTime.new(2000, 1, 1, 0, 0, 0), { 'f' => 5 }] + @hash1 = { a: 1, b: { c: 2 }, d: 'test', e: @sub_array1 } + @hash2 = { 'a' => 1, 'b' => { 'c' => 2 }, :d => 'test', 'e' => @sub_array2 } end it "should allow converting all values to strings recursively" do - expect(@hash1.stringify_values_recursively).to eq({:a => "1", :b => {:c => "2"}, :d => "test", :e => ["3", '4.0', "2000-01-01", '2000-01-01T00:00:00+00:00', {:f => "5"}]}) + expect(@hash1.stringify_values_recursively).to eq( + { + a: "1", b: { c: "2" }, d: "test", + e: ["3", '4.0', "2000-01-01", '2000-01-01T00:00:00+00:00', { f: "5" }] + } + ) end it "should allow converting all keys to symbols recursively" do @@ -18,29 +25,33 @@ end it "should allow converting keys" do - expect(@hash1.convert_keys(&:to_s)).to eq({"a" => 1, "b" => {:c => 2}, "d" => "test", "e" => @sub_array1}) + expect(@hash1.convert_keys(&:to_s)).to eq( + { "a" => 1, "b" => { c: 2 }, "d" => "test", "e" => @sub_array1 } + ) end it "should allow converting values" do - expect(@hash1.convert_values(&:to_s)).to eq({:a => "1", :b => {:c => 2}, :d => "test", :e => @sub_array1}) + expect(@hash1.convert_values(&:to_s)).to eq({ a: "1", b: { c: 2 }, d: "test", e: @sub_array1 }) end - + it "should allow converting values only for specific keys" do - expect(@hash1.convert_values(:d, :e, &:to_s)).to eq({:a => 1, :b => {:c => 2}, :d => "test", :e => @sub_array1}) + expect(@hash1.convert_values(:d, :e, &:to_s)).to eq( + { a: 1, b: { c: 2 }, d: "test", e: @sub_array1 } + ) end it "should allow making indifferent access recursively" do expect(@hash1.make_indifferent_access_recursively['b']['c']).to eq 2 expect(@hash1.make_indifferent_access_recursively['e'][4]['f']).to eq 5 end - + it "should allow executing blocks recursively" do - hash = {:a => 1, :b => {:a => 2}, :c => {:a => 3, :b => 4, :c => {:a => 5}}} + hash = { a: 1, b: { a: 2 }, c: { a: 3, b: 4, c: { a: 5 } } } result = [] - hash.recursively do |k,v| + hash.recursively do |k, v| result << v unless v.is_a?(Hash) - end - expect(result.sort).to eq [1,2,3,4,5] # Ruby 1.8.7 doesn't order hash keys + end + expect(result.sort).to eq [1, 2, 3, 4, 5] # Ruby 1.8.7 doesn't order hash keys end end @@ -49,36 +60,36 @@ describe Hash do it 'should allow removing all nil values and return a new hash' do - expect({:a => 1, :b => nil}.compact).to eq({:a => 1}) + expect({ a: 1, b: nil }.compact).to eq({ a: 1 }) end it 'should allow removing all nil values' do - a = {:a => 1, :b => nil} + a = { a: 1, b: nil } a.compact! - expect(a).to eq({:a => 1}) + expect(a).to eq({ a: 1 }) end it 'should allow removing all nil values and return a new hash' do - expect({:a => 1, :b => ''}.compact_blank).to eq({:a => 1}) + expect({ a: 1, b: '' }.compact_blank).to eq({ a: 1 }) end it 'should allow removing all blank values' do - a = {:a => 1, :b => ''} + a = { a: 1, b: '' } a.compact_blank! - expect(a).to eq({:a => 1}) + expect(a).to eq({ a: 1 }) end it 'should allow removing all blank values recursively' do - a = {:a => 1, :b => {:c => 1, :d => '', :e => []}} + a = { a: 1, b: { c: 1, d: '', e: [] } } a.recursive_compact_blank! - expect(a).to eq({:a => 1, :b => {:c => 1}}) + expect(a).to eq({ a: 1, b: { c: 1 } }) end it 'should allow extracting subsets' do - a = {:a => 1, :b => 2, :c => 3} + a = { a: 1, b: 2, c: 3 } b = a.extract!(:a, :c) - expect(b).to eq({:a => 1, :c => 3}) - expect(a).to eq({:b => 2}) + expect(b).to eq({ a: 1, c: 3 }) + expect(a).to eq({ b: 2 }) end end @@ -86,22 +97,22 @@ describe Hash, '#map_key_value' do - subject { {'1' => '2', 3 => 4} } + subject { { '1' => '2', 3 => 4 } } it 'should map key' do - expect(subject.map_key(:to_i)).to eq({1 => '2', 3 => 4}) + expect(subject.map_key(:to_i)).to eq({ 1 => '2', 3 => 4 }) end it 'should map value' do - expect(subject.map_value(:to_i)).to eq({'1' => 2, 3 => 4}) + expect(subject.map_value(:to_i)).to eq({ '1' => 2, 3 => 4 }) end it 'should map key and value' do - expect(subject.map_key_value(:to_i, :to_i)).to eq({1 => 2, 3 => 4}) + expect(subject.map_key_value(:to_i, :to_i)).to eq({ 1 => 2, 3 => 4 }) end it 'should map key and value if value not specified' do - expect(subject.map_key_value(:to_i)).to eq({1 => 2, 3 => 4}) + expect(subject.map_key_value(:to_i)).to eq({ 1 => 2, 3 => 4 }) end end diff --git a/spec/object_spec.rb b/spec/object_spec.rb index 885eacf..8e1a3a3 100644 --- a/spec/object_spec.rb +++ b/spec/object_spec.rb @@ -26,14 +26,13 @@ def id; 1; end class ReadyError < StandardError; end class BooleanizeTest - attr_accessor :ready def verify! - fail ArgumentError, "Ready should be a boolean" unless ready.is_a?(TrueClass) || ready.is_a?(FalseClass) + fail ArgumentError, "Ready should be a boolean" unless ready.is_a?(TrueClass) || + ready.is_a?(FalseClass) fail ReadyError, "Not ready" unless ready end - end @@ -42,8 +41,8 @@ def verify! end - it "should allow defining methods that will return boolean depending on the execution of another method" do - expect { @object.booleanize(:verify!, :rescue => ReadyError) }.to_not raise_error + it "allows defining methods that return boolean depending on the execution of another method" do + expect { @object.booleanize(:verify!, rescue: ReadyError) }.to_not raise_error expect { @object.verify? }.to raise_error(ArgumentError, 'Ready should be a boolean') @object.ready = false expect { @object.verify? }.to_not raise_error diff --git a/spec/string_spec.rb b/spec/string_spec.rb index 1f70b1c..10b5f64 100644 --- a/spec/string_spec.rb +++ b/spec/string_spec.rb @@ -1,7 +1,9 @@ describe String do it "should convert to underscore replacing spaces with underscores" do - expect("CamelCase UPPERCASE to be_Converted".proper_underscore).to eq "camel_case_uppercase_to_be_converted" + expect("CamelCase UPPERCASE to be_Converted".proper_underscore).to eq( + "camel_case_uppercase_to_be_converted" + ) end it 'should separate numbers and letters' do From a353864c899f1a7f2f7c9a43f5e310a4b55d4e56 Mon Sep 17 00:00:00 2001 From: Sean Earle Date: Wed, 16 May 2018 16:31:00 +0930 Subject: [PATCH 3/3] Changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c1bdaa..8b5b467 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ This changelog adheres to [Keep a CHANGELOG](http://keepachangelog.com/). ## Unreleased +### Added + +- [TT-4020] Implemented Rubocop + ## 0.0.1 ### Added