Skip to content

Commit

Permalink
Rubocop-suggested fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kspurgin committed Jan 13, 2024
1 parent 1483d10 commit 39f4ffc
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 70 deletions.
11 changes: 5 additions & 6 deletions bin/rspec
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 1 addition & 5 deletions lib/emendate/all_short_mdy_analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 5 additions & 13 deletions lib/emendate/date_part_tagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions lib/emendate/date_types/granularity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module DateTypes
module Granularity
extend self

Registry = {
REGISTRY = {
century: :year,
decade: :year,
millennium: :year,
Expand All @@ -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:)
Expand Down
16 changes: 9 additions & 7 deletions lib/emendate/examples/row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions lib/emendate/examples/testable_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions lib/emendate/examples/tester.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:)
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions lib/emendate/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 2 additions & 3 deletions lib/emendate/segment/number_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/emendate/unprocessable_tagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def call(...)
end
end

Patterns = [
PATTERNS = [
/^y-\d+$/,
/xxxx-\d{2}-xx/,
/\dxxx-xx/,
Expand All @@ -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
Expand Down
14 changes: 7 additions & 7 deletions spec/emendate/result_editable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
14 changes: 7 additions & 7 deletions spec/emendate/segment/derived_segment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down

0 comments on commit 39f4ffc

Please sign in to comment.