From d0d7fb85608df996f5ff77dda64245300b33e3c0 Mon Sep 17 00:00:00 2001 From: Matthew Rudy Jacobs Date: Tue, 20 Jun 2017 17:03:34 +0100 Subject: [PATCH] rubocop -a --- Rakefile | 11 +-- lib/memoist.rb | 25 +++--- lib/memoist/core_ext/singleton_class.rb | 11 ++- lib/memoist/version.rb | 3 +- memoist.gemspec | 51 +++++------ script/benchmark.rb | 8 +- test/memoist_test.rb | 109 ++++++++++++------------ test/test_helper.rb | 2 +- 8 files changed, 110 insertions(+), 110 deletions(-) diff --git a/Rakefile b/Rakefile index 08d5270..90873fe 100644 --- a/Rakefile +++ b/Rakefile @@ -1,11 +1,12 @@ # encoding: utf-8 -require "bundler/gem_tasks" -require "rake/testtask" +require 'bundler/gem_tasks' + +require 'rake/testtask' Rake::TestTask.new do |t| - t.libs << "test" - t.test_files = FileList["test/**/*_test.rb"] + t.libs << 'test' + t.test_files = FileList['test/**/*_test.rb'] t.verbose = true end -task :default => ["test"] +task default: ['test'] diff --git a/lib/memoist.rb b/lib/memoist.rb index 4803b58..00907c0 100644 --- a/lib/memoist.rb +++ b/lib/memoist.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true + require 'memoist/version' require 'memoist/core_ext/singleton_class' module Memoist - def self.extended(extender) Memoist.memoist_eval(extender) do unless singleton_class.method_defined?(:memoized_methods) @@ -14,27 +14,27 @@ def self.memoized_methods end end - def self.memoized_ivar_for(method_name, identifier=nil) + def self.memoized_ivar_for(method_name, identifier = nil) "@#{memoized_prefix(identifier)}_#{escape_punctuation(method_name)}" end - def self.unmemoized_method_for(method_name, identifier=nil) + def self.unmemoized_method_for(method_name, identifier = nil) "#{unmemoized_prefix(identifier)}_#{method_name}".to_sym end - def self.memoized_prefix(identifier=nil) + def self.memoized_prefix(identifier = nil) if identifier "_memoized_#{identifier}" else - "_memoized".freeze + '_memoized'.freeze end end - def self.unmemoized_prefix(identifier=nil) + def self.unmemoized_prefix(identifier = nil) if identifier "_unmemoized_#{identifier}" else - "_unmemoized".freeze + '_unmemoized'.freeze end end @@ -76,7 +76,7 @@ def unmemoize_all end def memoized_structs(names) - ref_obj = self.class.respond_to?(:class_eval) ? self.singleton_class : self + ref_obj = self.class.respond_to?(:class_eval) ? singleton_class : self structs = ref_obj.all_memoized_structs return structs if names.empty? @@ -111,7 +111,7 @@ def all_memoized_structs # an ancestor method. ancestors.grep(Memoist).each do |ancestor| ancestor.memoized_methods.each do |m| - structs << m unless structs.any? {|am| am.memoized_method == m.memoized_method } + structs << m unless structs.any? { |am| am.memoized_method == m.memoized_method } end end structs @@ -123,9 +123,7 @@ def clear_structs end def memoize(*method_names) - if method_names.last.is_a?(Hash) - identifier = method_names.pop[:identifier] - end + identifier = method_names.pop[:identifier] if method_names.last.is_a?(Hash) method_names.each do |method_name| unmemoized_method = Memoist.unmemoized_method_for(method_name, identifier) @@ -141,7 +139,7 @@ def memoize(*method_names) alias_method unmemoized_method, method_name mm = MemoizedMethod.new(method_name, memoized_ivar, instance_method(method_name).arity) - self.memoized_methods << mm + memoized_methods << mm if mm.arity == 0 # define a method like this; @@ -238,5 +236,4 @@ def #{method_name}(*args) # return a chainable method_name symbol if we can method_names.length == 1 ? method_names.first : method_names end - end diff --git a/lib/memoist/core_ext/singleton_class.rb b/lib/memoist/core_ext/singleton_class.rb index 9f0aaea..cfaa800 100644 --- a/lib/memoist/core_ext/singleton_class.rb +++ b/lib/memoist/core_ext/singleton_class.rb @@ -1,9 +1,12 @@ # frozen_string_literal: true + module Kernel # Returns the object's singleton class. - def singleton_class - class << self - self + unless respond_to?(:singleton_class) + def singleton_class + class << self + self + end end - end unless respond_to?(:singleton_class) # exists in 1.9.2 + end # exists in 1.9.2 end diff --git a/lib/memoist/version.rb b/lib/memoist/version.rb index e7acc3e..a2913c8 100644 --- a/lib/memoist/version.rb +++ b/lib/memoist/version.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + module Memoist - VERSION = "0.15.0" + VERSION = '0.15.0'.freeze end diff --git a/memoist.gemspec b/memoist.gemspec index eccea2f..7f63404 100644 --- a/memoist.gemspec +++ b/memoist.gemspec @@ -1,43 +1,44 @@ # coding: utf-8 + lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'memoist/version' AUTHORS = [ - ["Joshua Peek", "josh@joshpeek.com"], - ["Tarmo Tänav", "tarmo@itech.ee"], - ["Jeremy Kemper", "jeremy@bitsweat.net"], - ["Eugene Pimenov", "libc@mac.com"], - ["Xavier Noria", "fxn@hashref.com"], - ["Niels Ganser", "niels@herimedia.co"], - ["Carl Lerche & Yehuda Katz", "wycats@gmail.com"], - ["jeem", "jeem@hughesorama.com"], - ["Jay Pignata", "john.pignata@gmail.com"], - ["Damien Mathieu", "42@dmathieu.com"], - ["José Valim", "jose.valim@gmail.com"], - ["Matthew Rudy Jacobs", "matthewrudyjacobs@gmail.com"], -] + ['Joshua Peek', 'josh@joshpeek.com'], + ['Tarmo Tänav', 'tarmo@itech.ee'], + ['Jeremy Kemper', 'jeremy@bitsweat.net'], + ['Eugene Pimenov', 'libc@mac.com'], + ['Xavier Noria', 'fxn@hashref.com'], + ['Niels Ganser', 'niels@herimedia.co'], + ['Carl Lerche & Yehuda Katz', 'wycats@gmail.com'], + ['jeem', 'jeem@hughesorama.com'], + ['Jay Pignata', 'john.pignata@gmail.com'], + ['Damien Mathieu', '42@dmathieu.com'], + ['José Valim', 'jose.valim@gmail.com'], + ['Matthew Rudy Jacobs', 'matthewrudyjacobs@gmail.com'] +].freeze Gem::Specification.new do |spec| - spec.name = "memoist" + spec.name = 'memoist' spec.version = Memoist::VERSION - spec.authors = AUTHORS.map{ |name, email| name } - spec.email = AUTHORS.map{ |name, email| email } - spec.summary = %q{memoize methods invocation} - spec.homepage = "https://github.com/matthewrudy/memoist" - spec.license = "MIT" + spec.authors = AUTHORS.map { |name, _email| name } + spec.email = AUTHORS.map { |_name, email| email } + spec.summary = 'memoize methods invocation' + spec.homepage = 'https://github.com/matthewrudy/memoist' + spec.license = 'MIT' spec.files = `git ls-files -z`.split("\x0") spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) - spec.require_paths = ["lib"] + spec.require_paths = ['lib'] - spec.add_development_dependency "benchmark-ips" - spec.add_development_dependency "bundler" + spec.add_development_dependency 'benchmark-ips' + spec.add_development_dependency 'bundler' if RUBY_VERSION < '1.9.3' - spec.add_development_dependency "rake", "~> 10.4" + spec.add_development_dependency 'rake', '~> 10.4' else - spec.add_development_dependency "rake" + spec.add_development_dependency 'rake' end - spec.add_development_dependency "minitest", "~> 5.10" + spec.add_development_dependency 'minitest', '~> 5.10' end diff --git a/script/benchmark.rb b/script/benchmark.rb index ce5b41e..7faefc4 100644 --- a/script/benchmark.rb +++ b/script/benchmark.rb @@ -1,4 +1,4 @@ -$:.unshift File.expand_path(File.dirname(__FILE__)+"/../lib") +$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib') require 'benchmark/ips' require 'memoist' @@ -7,7 +7,7 @@ class Benchy extend Memoist def arity_0 - "Hello World" + 'Hello World' end memoize :arity_0 @@ -22,7 +22,7 @@ def arity_1(name) puts "Benchmarking: #{Memoist::VERSION}" Benchmark.ips do |x| - x.report("arity 0 - memoized") do |times| + x.report('arity 0 - memoized') do |times| times.times do OBJECT.arity_0 end @@ -34,7 +34,7 @@ def arity_1(name) # end # end - x.report("arity 1 - memoized") do |times| + x.report('arity 1 - memoized') do |times| times.times do OBJECT.arity_1(:World) end diff --git a/test/memoist_test.rb b/test/memoist_test.rb index 098fa3e..49a09d0 100644 --- a/test/memoist_test.rb +++ b/test/memoist_test.rb @@ -2,9 +2,7 @@ require 'memoist' class MemoistTest < Minitest::Test - class CallCounter - def initialize @calls = {} end @@ -17,7 +15,6 @@ def call(method_name) def count(method_name) @calls[method_name] ||= 0 end - end class Person @@ -49,7 +46,7 @@ def age_calls def name @counter.call(:name) - "Josh" + 'Josh' end def name? @@ -58,8 +55,8 @@ def name? end memoize :name? - def update(name) - "Joshua" + def update(_name) + 'Joshua' end memoize :update @@ -80,7 +77,7 @@ def sleep_calls @counter.count(:sleep) end - def update_attributes(options = {}) + def update_attributes(_options = {}) @counter.call(:update_attributes) true end @@ -101,7 +98,7 @@ def memoize_protected_test def is_developer? @counter.call(:is_developer?) - "Yes" + 'Yes' end memoize :is_developer? end @@ -111,12 +108,12 @@ def name @counter.call(:student_name) "Student #{super}" end - memoize :name, :identifier => :student + memoize :name, identifier: :student end class Teacher < Person def seniority - "very_senior" + 'very_senior' end memoize :seniority end @@ -129,7 +126,7 @@ def initialize def name @name_calls += 1 - "37signals" + '37signals' end end @@ -183,9 +180,9 @@ def counter class Book extend Memoist - STATUSES = %w(new used) - CLASSIFICATION = %w(fiction nonfiction) - GENRES = %w(humor romance reference sci-fi classic philosophy) + STATUSES = %w[new used].freeze + CLASSIFICATION = %w[fiction nonfiction].freeze + GENRES = %w[humor romance reference sci-fi classic philosophy].freeze attr_reader :title, :author def initialize(title, author) @@ -202,7 +199,7 @@ class << self extend Memoist def all_types - STATUSES.product(CLASSIFICATION).product(GENRES).collect { |a| a.flatten } + STATUSES.product(CLASSIFICATION).product(GENRES).collect(&:flatten) end memoize :all_types end @@ -211,7 +208,7 @@ def all_types class Abb extend Memoist - def run(*args) + def run(*_args) flush_cache if respond_to?(:flush_cache) execute end @@ -226,7 +223,6 @@ def some_method end class Bbb < Abb - def some_method :foo end @@ -236,14 +232,14 @@ def some_method def setup @person = Person.new @calculator = Calculator.new - @book = Book.new("My Life", "Brian 'Fudge' Turmuck") + @book = Book.new('My Life', "Brian 'Fudge' Turmuck") end def test_memoization - assert_equal "Josh", @person.name + assert_equal 'Josh', @person.name assert_equal 1, @person.name_calls - 3.times { assert_equal "Josh", @person.name } + 3.times { assert_equal 'Josh', @person.name } assert_equal 1, @person.name_calls end @@ -259,13 +255,13 @@ def test_memoize_with_optional_arguments end def test_memoize_with_options_hash - assert_equal true, @person.update_attributes(:age => 21, :name => 'James') + assert_equal true, @person.update_attributes(age: 21, name: 'James') assert_equal 1, @person.update_attributes_calls - 3.times { assert_equal true, @person.update_attributes(:age => 21, :name => 'James') } + 3.times { assert_equal true, @person.update_attributes(age: 21, name: 'James') } assert_equal 1, @person.update_attributes_calls - 3.times { assert_equal true, @person.update_attributes({:age => 21, :name => 'James'}, :reload) } + 3.times { assert_equal true, @person.update_attributes({ age: 21, name: 'James' }, :reload) } assert_equal 4, @person.update_attributes_calls end @@ -323,12 +319,12 @@ def test_class_flush_cache_preserves_instances @book.memoize_all Book.memoize_all assert_equal "My Life by Brian 'Fudge' Turmuck", @book.full_title - + Book.flush_cache assert_equal false, Book.instance_variable_defined?(:@_memoized_all_types) assert_equal "My Life by Brian 'Fudge' Turmuck", @book.full_title end - + def test_flush_cache_in_child_class x = Bbb.new @@ -352,20 +348,20 @@ def test_all_memoized_structs # Student < Person memoize :name, :identifier => :student # Teacher < Person memoize :seniority - expected = %w(age is_developer? memoize_protected_test name name? sleep update update_attributes) + expected = %w[age is_developer? memoize_protected_test name name? sleep update update_attributes] structs = Person.all_memoized_structs assert_equal expected, structs.collect(&:memoized_method).collect(&:to_s).sort - assert_equal "@_memoized_name", structs.detect {|s| s.memoized_method == :name }.ivar + assert_equal '@_memoized_name', structs.detect { |s| s.memoized_method == :name }.ivar # Same expected methods structs = Student.all_memoized_structs assert_equal expected, structs.collect(&:memoized_method).collect(&:to_s).sort - assert_equal "@_memoized_student_name", structs.detect {|s| s.memoized_method == :name }.ivar + assert_equal '@_memoized_student_name', structs.detect { |s| s.memoized_method == :name }.ivar - expected = (expected << "seniority").sort + expected = (expected << 'seniority').sort structs = Teacher.all_memoized_structs assert_equal expected, structs.collect(&:memoized_method).collect(&:to_s).sort - assert_equal "@_memoized_name", structs.detect {|s| s.memoized_method == :name }.ivar + assert_equal '@_memoized_name', structs.detect { |s| s.memoized_method == :name }.ivar end def test_unmemoize_all_subclasses @@ -374,18 +370,18 @@ def test_unmemoize_all_subclasses # Teacher < Person memoize :seniority teacher = Teacher.new - assert_equal "Josh", teacher.name - assert_equal "Josh", teacher.instance_variable_get(:@_memoized_name) - assert_equal "very_senior", teacher.seniority - assert_equal "very_senior", teacher.instance_variable_get(:@_memoized_seniority) + assert_equal 'Josh', teacher.name + assert_equal 'Josh', teacher.instance_variable_get(:@_memoized_name) + assert_equal 'very_senior', teacher.seniority + assert_equal 'very_senior', teacher.instance_variable_get(:@_memoized_seniority) teacher.unmemoize_all assert_equal false, teacher.instance_variable_defined?(:@_memoized_name) assert_equal false, teacher.instance_variable_defined?(:@_memoized_seniority) student = Student.new - assert_equal "Student Josh", student.name - assert_equal "Student Josh", student.instance_variable_get(:@_memoized_student_name) + assert_equal 'Student Josh', student.name + assert_equal 'Student Josh', student.instance_variable_get(:@_memoized_student_name) assert_equal false, student.instance_variable_defined?(:@_memoized_seniority) student.unmemoize_all @@ -405,14 +401,14 @@ def test_memoize_all_subclasses teacher = Teacher.new teacher.memoize_all - assert_equal "very_senior", teacher.instance_variable_get(:@_memoized_seniority) - assert_equal "Josh", teacher.instance_variable_get(:@_memoized_name) + assert_equal 'very_senior', teacher.instance_variable_get(:@_memoized_seniority) + assert_equal 'Josh', teacher.instance_variable_get(:@_memoized_name) student = Student.new student.memoize_all - assert_equal "Student Josh", student.instance_variable_get(:@_memoized_student_name) - assert_equal "Student Josh", student.name + assert_equal 'Student Josh', student.instance_variable_get(:@_memoized_student_name) + assert_equal 'Student Josh', student.name assert_equal false, student.instance_variable_defined?(:@_memoized_seniority) end @@ -434,8 +430,8 @@ def test_memoization_class_variables def test_memoized_is_not_affected_by_freeze @person.freeze - assert_equal "Josh", @person.name - assert_equal "Joshua", @person.update("Joshua") + assert_equal 'Josh', @person.name + assert_equal 'Joshua', @person.update('Joshua') end def test_memoization_with_args @@ -462,9 +458,9 @@ def test_object_memoization company.extend Memoist company.memoize :name - assert_equal "37signals", company.name + assert_equal '37signals', company.name assert_equal 1, company.name_calls - assert_equal "37signals", company.name + assert_equal '37signals', company.name assert_equal 1, company.name_calls end end @@ -495,9 +491,9 @@ def test_double_memoization_with_identifier # Student < Person memoize :name, :identifier => :student # Teacher < Person memoize :seniority - Person.memoize :name, :identifier => :again + Person.memoize :name, identifier: :again p = Person.new - assert_equal "Josh", p.name + assert_equal 'Josh', p.name assert p.instance_variable_get(:@_memoized_again_name) # HACK: tl;dr: Don't memoize classes in test that are used elsewhere. @@ -508,12 +504,12 @@ def test_double_memoization_with_identifier Student.all_memoized_structs Person.all_memoized_structs Teacher.all_memoized_structs - assert Person.memoized_methods.any? { |m| m.ivar == "@_memoized_again_name" } + assert Person.memoized_methods.any? { |m| m.ivar == '@_memoized_again_name' } - [Student, Teacher, Person].each { |obj| obj.clear_structs } - assert Person.memoized_methods.reject! { |m| m.ivar == "@_memoized_again_name" } - assert_nil Student.memoized_methods.reject! { |m| m.ivar == "@_memoized_again_name" } - assert_nil Teacher.memoized_methods.reject! { |m| m.ivar == "@_memoized_again_name" } + [Student, Teacher, Person].each(&:clear_structs) + assert Person.memoized_methods.reject! { |m| m.ivar == '@_memoized_again_name' } + assert_nil Student.memoized_methods.reject! { |m| m.ivar == '@_memoized_again_name' } + assert_nil Teacher.memoized_methods.reject! { |m| m.ivar == '@_memoized_again_name' } end def test_memoization_with_a_subclass @@ -526,7 +522,9 @@ def test_memoization_with_a_subclass def test_memoization_is_chainable klass = Class.new do - def foo; "bar"; end + def foo + 'bar' + end end klass.extend Memoist chainable = klass.memoize :foo @@ -537,17 +535,16 @@ def test_protected_method_memoization person = Person.new assert_raises(NoMethodError) { person.memoize_protected_test } - assert_equal "protected", person.send(:memoize_protected_test) + assert_equal 'protected', person.send(:memoize_protected_test) end def test_private_method_memoization person = Person.new assert_raises(NoMethodError) { person.is_developer? } - assert_equal "Yes", person.send(:is_developer?) + assert_equal 'Yes', person.send(:is_developer?) assert_equal 1, person.is_developer_calls - assert_equal "Yes", person.send(:is_developer?) + assert_equal 'Yes', person.send(:is_developer?) assert_equal 1, person.is_developer_calls end - end diff --git a/test/test_helper.rb b/test/test_helper.rb index 37d2728..6e0bc6f 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,3 +1,3 @@ require 'minitest/autorun' -$:.unshift File.expand_path(File.dirname(__FILE__)+"/../lib") +$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')