Skip to content

Commit

Permalink
Quit using a class instance variable
Browse files Browse the repository at this point in the history
  • Loading branch information
makicamel committed Oct 15, 2023
1 parent 85461b1 commit 024f6c5
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/bulletmark_repairer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'bulletmark_repairer/version'
require 'bulletmark_repairer/railtie' if ENV['REPAIR']
require 'bulletmark_repairer/bulletmark_repairer'
require 'bulletmark_repairer/application_associations'
require 'bulletmark_repairer/associations_builder'
require 'bulletmark_repairer/configuration'
require 'bulletmark_repairer/corrector_builder'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
# frozen_string_literal: true

module BulletmarkRepairer
def self.associations
@associations ||= ApplicationAssociations.new
end

def self.key(target_klass_name, base_klass_name, candidates)
associations.key(target_klass_name, base_klass_name, candidates)
end

def self.reset_associations
@associations = nil
end

class ApplicationAssociations
def key(target_klass_name, base_klass_name, candidates)
key = target_klass_name.underscore
Expand Down
19 changes: 13 additions & 6 deletions lib/bulletmark_repairer/associations_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ def build(marker)
if associations[marker.index]
associations[marker.index].add(marker)
else
associations[marker.index] = Associations.new(marker)
associations[marker.index] = Associations.new(marker, @application_associations)
end
end

def associations
@associations ||= {}
end

private

def initialize
@application_associations = BulletmarkRepairer::ApplicationAssociations.new
end
end

class Associations
Expand All @@ -38,9 +44,10 @@ def corrector(dir)

private

def initialize(marker)
def initialize(marker, application_associations)
@marker = marker
@associations = { base: marker.associations }
@application_associations = application_associations
end

# @return [Hash, nil]
Expand All @@ -66,12 +73,12 @@ def build_associations!(marker:, associations:, parent_keys:)
def formed_key(marker:, associations:)
case associations
when Hash
BulletmarkRepairer.key(marker.base_class, @marker.base_class, associations.keys) ||
BulletmarkRepairer.key(marker.base_class, @marker.base_class, associations.values.flatten)
@application_associations.key(marker.base_class, @marker.base_class, associations.keys) ||
@application_associations.key(marker.base_class, @marker.base_class, associations.values.flatten)
when Array
BulletmarkRepairer.key(marker.base_class, @marker.base_class, associations)
@application_associations.key(marker.base_class, @marker.base_class, associations)
else # Symbol, String
BulletmarkRepairer.key(marker.base_class, @marker.base_class, [associations])
@application_associations.key(marker.base_class, @marker.base_class, [associations])
end
end

Expand Down
1 change: 0 additions & 1 deletion lib/bulletmark_repairer/rack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def call(env)
end

def _call(env)
BulletmarkRepairer.reset_associations
@app.call(env)
ensure
begin
Expand Down
3 changes: 2 additions & 1 deletion spec/associations_builder/associations_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

RSpec.describe BulletmarkRepairer::Associations do
describe '#build_associations!' do
let(:associations) { described_class.new(parent_marker) }
let(:associations) { described_class.new(parent_marker, application_associations) }
let(:application_associations) { BulletmarkRepairer::ApplicationAssociations.new }
let(:parent_marker) { double(:marker, base_class: parent_attributes.keys.first, associations: parent_attributes.values.first) }
let(:child_marker) { double(:marker, base_class: child_attributes.keys.first, associations: child_attributes.values.first) }

Expand Down

0 comments on commit 024f6c5

Please sign in to comment.