Commit 1f0d0771 authored by Doug Barrett's avatar Doug Barrett 🔴
Browse files

refactor(fields): Use hash lookup for deprecated field mappings

Change Deprecated.all to return a hash mapping deprecated field names
to their standard equivalents, enabling O(1) lookups. Remove unused
deprecated_for and mappings methods. Update tests accordingly.
parent 2f64d944
Loading
Loading
Loading
Loading
+7 −23
Original line number Diff line number Diff line
@@ -79,11 +79,13 @@ module Labkit
      }.freeze

      class << self
        # Get all deprecated fields
        # Get all deprecated fields as a lookup hash
        #
        # @return [Array<String>] List of all deprecated field names
        # @return [Hash{String => String}] Hash mapping deprecated field names to standard field names
        def all
          MAPPINGS.values.flatten
          @all = MAPPINGS.each_with_object({}) do |(key, values), result| 
            values.each { |v| result[v] = key }
          end
        end

        # Check if a field is deprecated
@@ -91,7 +93,7 @@ module Labkit
        # @param field_name [String, Symbol] The field name to check
        # @return [Boolean] true if the field is deprecated
        def deprecated?(field_name)
          all.include?(field_name.to_s)
          all.key?(field_name.to_s)
        end

        # Get the standard field for a deprecated field
@@ -99,25 +101,7 @@ module Labkit
        # @param deprecated_field [String, Symbol] The deprecated field name
        # @return [String, nil] The standard field name, or nil if not found
        def standard_field_for(deprecated_field)
          MAPPINGS.each do |standard, deprecated_list|
            return standard if deprecated_list.include?(deprecated_field.to_s)
          end
          nil
        end

        # Get all deprecated fields for a standard field
        #
        # @param standard_field [String] The standard field name
        # @return [Array<String>] List of deprecated field names
        def deprecated_for(standard_field)
          MAPPINGS[standard_field] || []
        end

        # Get all mappings
        #
        # @return [Hash] The complete mapping hash
        def mappings
          MAPPINGS
          all[deprecated_field.to_s]
        end
      end
    end
+8 −9
Original line number Diff line number Diff line
@@ -12,9 +12,14 @@ RSpec.describe Labkit::Fields do

  describe Labkit::Fields::Deprecated do
    describe '.all' do
      it 'returns an array of deprecated field names' do
        expect(described_class.all).to be_an(Array)
        expect(described_class.all).to all(be_a(String))
      it 'returns a hash mapping deprecated field names to standard field names' do
        expect(described_class.all).to be_a(Hash)
        expect(described_class.all.keys).to all(be_a(String))
        expect(described_class.all.values).to all(be_a(String))
      end

      it 'maps deprecated fields to their standard equivalents' do
        expect(described_class.all['meta.user_id']).to eq(Labkit::Fields::GL_USER_ID)
      end
    end

@@ -37,11 +42,5 @@ RSpec.describe Labkit::Fields do
      end
    end

    describe '.mappings' do
      it 'returns a frozen hash' do
        expect(described_class.mappings).to be_a(Hash)
        expect(described_class.mappings).to be_frozen
      end
    end
  end
end