Class: ActiveVersion::VersionRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/active_version/version_registry.rb,
sig/active_version/registry_and_mapping.rbs

Overview

Registry for tracking versioned models and their configuration

Instance Method Summary collapse

Constructor Details

#initializeVersionRegistry

Returns a new instance of VersionRegistry.



4
5
6
7
# File 'lib/active_version/version_registry.rb', line 4

def initialize
  @models = {}
  @version_classes = {}
end

Instance Method Details

#clear!void

This method returns an undefined value.

Clear all registrations (useful for testing)



78
79
80
81
# File 'lib/active_version/version_registry.rb', line 78

def clear!
  @models.clear
  @version_classes.clear
end

#config_for(model_class, version_type) ⇒ Hash[Symbol, untyped]?

Get configuration for a model and version type

Parameters:

  • model_class (Class)
  • version_type (Symbol)

Returns:

  • (Hash[Symbol, untyped], nil)


64
65
66
67
# File 'lib/active_version/version_registry.rb', line 64

def config_for(model_class, version_type)
  key = registry_key(model_class, version_type)
  @models[key]&.fetch(:options, {})
end

#config_for_model_name(model_name, version_type) ⇒ Hash[Symbol, untyped]?

Get configuration by model class name and version type. Useful while constants are still being wired and only the intended class name is known.

Parameters:

  • model_name (String)
  • version_type (Symbol)

Returns:

  • (Hash[Symbol, untyped], nil)


72
73
74
75
# File 'lib/active_version/version_registry.rb', line 72

def config_for_model_name(model_name, version_type)
  key = :"#{model_name}:#{version_type}"
  @models[key]&.fetch(:options, {})
end

#models_for_version_type(version_type) ⇒ Array[Class]

Get all registered models for a version type

Parameters:

  • version_type (Symbol)

Returns:

  • (Array[Class])


58
59
60
61
# File 'lib/active_version/version_registry.rb', line 58

def models_for_version_type(version_type)
  @models.select { |_k, v| v[:version_type] == version_type }
    .map { |_k, v| v[:model_class] }
end

#register(model_class, version_type, options = {}) ⇒ Hash[Symbol, untyped]

Register a model with versioning Detects conflicts when re-registering with different options or duplicate registrations

Parameters:

  • model_class (Class)
  • version_type (Symbol)
  • options (Hash[Symbol, untyped]) (defaults to: {})

Returns:

  • (Hash[Symbol, untyped])


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/active_version/version_registry.rb', line 11

def register(model_class, version_type, options = {})
  key = registry_key(model_class, version_type)

  # Check for existing registration
  if @models.key?(key)
    existing = @models[key]

    # Detect option conflicts
    if existing[:options] != options
      ActiveVersion.logger&.warn(
        "[ActiveVersion] Re-registering #{model_class.name} with :#{version_type} " \
        "with different options. Previous: #{existing[:options].inspect}, " \
        "New: #{options.inspect}. This may indicate a configuration issue."
      )
    else
      # Same options - likely a double include, but not necessarily a problem
      # Log at debug level if needed
    end
  end

  @models[key] = {
    model_class: model_class,
    version_type: version_type,
    options: options,
    registered_at: Time.current
  }
end

#register_version_class(model_class, version_type, version_class) ⇒ Class

Register a version class

Parameters:

  • model_class (Class)
  • version_type (Symbol)
  • version_class (Class)

Returns:

  • (Class)


46
47
48
49
# File 'lib/active_version/version_registry.rb', line 46

def register_version_class(model_class, version_type, version_class)
  key = registry_key(model_class, version_type)
  @version_classes[key] = version_class
end

#registered?(model_class, version_type) ⇒ Boolean

Check if a model is registered for versioning

Parameters:

  • model_class (Class)
  • version_type (Symbol)

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/active_version/version_registry.rb', line 52

def registered?(model_class, version_type)
  key = registry_key(model_class, version_type)
  @models.key?(key)
end

#version_class_for(model_class, version_type) ⇒ Class?

Get version class for a model and version type

Parameters:

  • model_class (Class)
  • version_type (Symbol)

Returns:

  • (Class, nil)


40
41
42
43
# File 'lib/active_version/version_registry.rb', line 40

def version_class_for(model_class, version_type)
  key = registry_key(model_class, version_type)
  @version_classes[key]
end