Class: ActiveVersion::ColumnMapper

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

Overview

Maps versioning concepts to actual column names Allows developers to configure any column name to any concept

Instance Method Summary collapse

Constructor Details

#initializeColumnMapper

Returns a new instance of ColumnMapper.



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

def initialize
  @mappings = {}
end

Instance Method Details

#column_for(model_class, version_type, concept) ⇒ Symbol, String

Get column name for a concept

Parameters:

  • model_class (Class)
  • version_type (Symbol)
  • concept (Symbol)

Returns:

  • (Symbol, String)


16
17
18
19
# File 'lib/active_version/column_mapper.rb', line 16

def column_for(model_class, version_type, concept)
  key = mapping_key(model_class, version_type, concept)
  @mappings[key] || default_column_for(version_type, concept)
end

#mappings_for(model_class, version_type) ⇒ Hash[Symbol, Symbol]

Get all mappings for a model and version type

Parameters:

  • model_class (Class)
  • version_type (Symbol)

Returns:

  • (Hash[Symbol, Symbol])


22
23
24
25
26
# File 'lib/active_version/column_mapper.rb', line 22

def mappings_for(model_class, version_type)
  prefix = "#{model_class.name}:#{version_type}:"
  @mappings.select { |k, _v| k.to_s.start_with?(prefix) }
    .transform_keys { |k| k.to_s.sub(prefix, "").to_sym }
end

#register(model_class, version_type, concept, column_name) ⇒ Symbol

Register a column mapping for a model and version type

Parameters:

  • model_class (Class)
  • version_type (Symbol)
  • concept (Symbol)
  • column_name (Symbol, String)

Returns:

  • (Symbol)


10
11
12
13
# File 'lib/active_version/column_mapper.rb', line 10

def register(model_class, version_type, concept, column_name)
  key = mapping_key(model_class, version_type, concept)
  @mappings[key] = column_name.to_sym
end