From 39f4ffcbd35c3e3ef942e89bb4e5144e33c86fb9 Mon Sep 17 00:00:00 2001 From: Kristina Spurgin Date: Fri, 12 Jan 2024 22:04:33 -0500 Subject: [PATCH] Rubocop-suggested fixes --- bin/rspec | 11 +++++------ lib/emendate/all_short_mdy_analyzer.rb | 6 +----- lib/emendate/date_part_tagger.rb | 18 +++++------------- lib/emendate/date_types/granularity.rb | 4 ++-- lib/emendate/examples/row.rb | 16 +++++++++------- lib/emendate/examples/testable_example.rb | 16 ++++++++-------- lib/emendate/examples/tester.rb | 13 +++++++------ lib/emendate/lexer.rb | 6 ++---- lib/emendate/segment/number_token.rb | 5 ++--- lib/emendate/unprocessable_tagger.rb | 4 ++-- spec/emendate/result_editable_spec.rb | 14 +++++++------- spec/emendate/segment/derived_segment_spec.rb | 14 +++++++------- 12 files changed, 57 insertions(+), 70 deletions(-) diff --git a/bin/rspec b/bin/rspec index 30d4692d..7a2b4516 100755 --- a/bin/rspec +++ b/bin/rspec @@ -18,12 +18,11 @@ if File.file?(bundle_binstub) if /This file was generated by Bundler/.match?(File.read(bundle_binstub, 300)) load(bundle_binstub) else - # rubocop:todo Layout/LineLength - abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. - # rubocop:enable Layout/LineLength -# rubocop:todo Layout/LineLength -Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") -# rubocop:enable Layout/LineLength + abort("Your `bin/bundle` was not generated by Bundler, so this binstub "\ + "cannot run.\n"\ + "Replace `bin/bundle` by running `bundle binstubs bundler --force`, "\ + "then run this command again.") + end end diff --git a/lib/emendate/all_short_mdy_analyzer.rb b/lib/emendate/all_short_mdy_analyzer.rb index d9a1fd87..3e447374 100644 --- a/lib/emendate/all_short_mdy_analyzer.rb +++ b/lib/emendate/all_short_mdy_analyzer.rb @@ -114,11 +114,7 @@ def transform_all_ambiguous def transform_ambiguous_pair(part) yr = transform_part(part[0], :year) - begin - analyzer = Emendate::MonthDayAnalyzer.call(part[1], part[2], yr) - rescue Emendate::Error => e - raise e - end + analyzer = Emendate::MonthDayAnalyzer.call(part[1], part[2], yr) transform_part(analyzer.month, :month) transform_part(analyzer.day, :day) diff --git a/lib/emendate/date_part_tagger.rb b/lib/emendate/date_part_tagger.rb index cbc578a8..abef736d 100644 --- a/lib/emendate/date_part_tagger.rb +++ b/lib/emendate/date_part_tagger.rb @@ -189,14 +189,9 @@ def tag_numeric_month_day_year num1, num2, yr = result.extract(%i[number1or2 number1or2 year]).segments - begin - res = Emendate::MonthDayAnalyzer.call(num1, num2, yr) - rescue Emendate::MonthDayError => e - raise e - else - replace_x_with_date_part_type(x: res.month, date_part_type: :month) - replace_x_with_date_part_type(x: res.day, date_part_type: :day) - end + res = Emendate::MonthDayAnalyzer.call(num1, num2, yr) + replace_x_with_date_part_type(x: res.month, date_part_type: :month) + replace_x_with_date_part_type(x: res.day, date_part_type: :day) res.warnings.each { |warn| result.warnings << warn } end @@ -215,11 +210,8 @@ def tag_year_in_season_short_year def tag_numeric_month_day_short_year to_convert = result.extract(%i[number1or2 hyphen number1or2 hyphen number1or2]) - begin - analyzer = Emendate::AllShortMdyAnalyzer.call(to_convert) - rescue Emendate::Error => e - raise(e) - end + + analyzer = Emendate::AllShortMdyAnalyzer.call(to_convert) analyzer.warnings.each { |warn| result.warnings << warn } replace_segments_with_new(segments: to_convert.segments, diff --git a/lib/emendate/date_types/granularity.rb b/lib/emendate/date_types/granularity.rb index 6e2e7a5e..286c642a 100644 --- a/lib/emendate/date_types/granularity.rb +++ b/lib/emendate/date_types/granularity.rb @@ -8,7 +8,7 @@ module DateTypes module Granularity extend self - Registry = { + REGISTRY = { century: :year, decade: :year, millennium: :year, @@ -24,7 +24,7 @@ module Granularity def granularity datetype = type.to_s.delete_suffix("_date_type").to_sym - Registry[datetype] + REGISTRY[datetype] end def granular_date(side:, date_type:) diff --git a/lib/emendate/examples/row.rb b/lib/emendate/examples/row.rb index 04e51320..d30a5494 100644 --- a/lib/emendate/examples/row.rb +++ b/lib/emendate/examples/row.rb @@ -4,11 +4,9 @@ module Emendate module Examples class Row def initialize(row) - prepped = prep(row) + @keys = prep(row).keys # metaprogramming bit to create an instance variable for each column - prepped.keys.each do |field| - instance_variable_set(:"@#{field}", row[field]) - end + keys.each { |field| instance_variable_set(:"@#{field}", row[field]) } end def data_sets @@ -49,11 +47,15 @@ def runnable_tests .keys end + def respond_to_missing?(method, *) + keys.include?(method.to_s) || super + end + private - # rubocop:todo Layout/LineLength - # metaprogramming bit to avoid manually declaring attr_reader for every column in row - # rubocop:enable Layout/LineLength + attr_reader :keys + + # avoid manually declaring attr_reader for every column in row def method_missing(symbol, *args) instance_variable_get(:"@#{symbol}") rescue diff --git a/lib/emendate/examples/testable_example.rb b/lib/emendate/examples/testable_example.rb index da342e73..52bf0b15 100644 --- a/lib/emendate/examples/testable_example.rb +++ b/lib/emendate/examples/testable_example.rb @@ -24,17 +24,17 @@ def initialize(rows) end def add_error(testname, err) - errors.key?(testname) ? @errors[testname] = - # rubocop:todo Layout/LineLength - "#{errors[testname]}|#{err}" : @errors[testname] = - # rubocop:enable Layout/LineLength - err + @errors[testname] = formatted_error(testname, err) + end + + def formatted_error(testname, err) + return err unless errors.key?(testname) + + "#{errors[testname]}|#{err}" end def add_test_result(testname, result) - test_results.key?(testname) ? @test_results[testname] = - result : @test_results[testname] = - result + @test_results[testname] = result end def all_tags diff --git a/lib/emendate/examples/tester.rb b/lib/emendate/examples/tester.rb index 9cef26f6..8e5f2e6b 100644 --- a/lib/emendate/examples/tester.rb +++ b/lib/emendate/examples/tester.rb @@ -14,9 +14,9 @@ class << self def build(test:, example:) split_test = test.split("_") type = split_test.shift - # rubocop:todo Layout/LineLength - test_type = Object.const_get("Emendate::Examples::#{type.capitalize}Testable") - # rubocop:enable Layout/LineLength + test_type = Object.const_get( + "Emendate::Examples::#{type.capitalize}Testable" + ) test_name = "#{type}_#{split_test.join("_")}" new(type: test_type, name: test_name, example: example) end @@ -26,9 +26,8 @@ def build(test:, example:) attr_reader :name - # rubocop:todo Layout/LineLength - # @param type [Constant] Module that will be mixed in to run test. Set by Tester.build from first part of test name - # rubocop:enable Layout/LineLength + # @param type [Constant] Module that will be mixed in to run test. Set by + # `Tester.build` from first part of test name # @param name [String] test name # @param example [Emendate::Examples::TestableExample] def initialize(type:, name:, example:) @@ -52,6 +51,8 @@ def method_missing(symbol, *args) super(symbol, *args) end + def respond_to_missing? = super + def to_s "#{self.class.name}, name: #{name}" end diff --git a/lib/emendate/lexer.rb b/lib/emendate/lexer.rb index 83203e3e..f57dff6c 100644 --- a/lib/emendate/lexer.rb +++ b/lib/emendate/lexer.rb @@ -76,10 +76,8 @@ def call(...) # If additional alphabetic seasons are added, make sure to # update the mapping to literals in Segments::SeasonAlphaToken /^(winter|spring|summer|fall|autumn)/i => :season, - # rubocop:todo Layout/LineLength - /^(date unknown|unknown date|no date|not dated|undated|unknown|unk|n\.? ?d\.?)$/i => - # rubocop:enable Layout/LineLength - :unknown_date, + /^(date\sunknown|unknown\sdate|no\sdate|not\sdated|undated| + unknown|unk|n\.?\s?d\.?)$/ix => :unknown_date, Regexp.new(ordinals, "i") => :ordinal_indicator, /^(u+|x+)/i => :uncertainty_digits } diff --git a/lib/emendate/segment/number_token.rb b/lib/emendate/segment/number_token.rb index 6c180631..c8dac0c9 100644 --- a/lib/emendate/segment/number_token.rb +++ b/lib/emendate/segment/number_token.rb @@ -30,9 +30,8 @@ def post_initialize(opts) unless lexeme.match?(/^\d+$/) raise Emendate::TokenLexemeError, - # rubocop:todo Layout/LineLength - "Number token must be created with lexeme containing only numeric digits" - # rubocop:enable Layout/LineLength + "Number token must be created with lexeme containing only numeric "\ + "digits" end @digits = opts[:digits] || default_digits diff --git a/lib/emendate/unprocessable_tagger.rb b/lib/emendate/unprocessable_tagger.rb index 8a9ec8ac..0f940fe3 100644 --- a/lib/emendate/unprocessable_tagger.rb +++ b/lib/emendate/unprocessable_tagger.rb @@ -12,7 +12,7 @@ def call(...) end end - Patterns = [ + PATTERNS = [ /^y-\d+$/, /xxxx-\d{2}-xx/, /\dxxx-xx/, @@ -22,7 +22,7 @@ def call(...) /^\d{3,4}S\d+$/, /\d{4}-\d{2}-xx/ ] - Re = Regexp.union(Patterns) + Re = Regexp.union(PATTERNS) def initialize(tokens) @tokens = tokens diff --git a/spec/emendate/result_editable_spec.rb b/spec/emendate/result_editable_spec.rb index 4710d796..d82ebbad 100644 --- a/spec/emendate/result_editable_spec.rb +++ b/spec/emendate/result_editable_spec.rb @@ -2,17 +2,17 @@ require "spec_helper" -RSpec.describe Emendate::ResultEditable do - class Editable - include Emendate::ResultEditable +class Editable + include Emendate::ResultEditable - attr_reader :result + attr_reader :result - def initialize(tokens) - @result = tokens - end + def initialize(tokens) + @result = tokens end +end +RSpec.describe Emendate::ResultEditable do describe "#collapse_segments_backward" do it "collapses as expected" do tokens = Emendate.prepped_for( diff --git a/spec/emendate/segment/derived_segment_spec.rb b/spec/emendate/segment/derived_segment_spec.rb index 95c20856..42452110 100644 --- a/spec/emendate/segment/derived_segment_spec.rb +++ b/spec/emendate/segment/derived_segment_spec.rb @@ -2,17 +2,17 @@ require "spec_helper" -RSpec.describe Emendate::DerivedSegment do - class Derivable < Emendate::Token - include Emendate::DerivedSegment +class Derivable < Emendate::Token + include Emendate::DerivedSegment - private + private - def post_initialize(opts) - derive(opts) - end + def post_initialize(opts) + derive(opts) end +end +RSpec.describe Emendate::DerivedSegment do let(:derived_type) { :newtype } let(:klass) { Derivable.new(type: derived_type, sources: sources) }