Class: ActiveVersion::ColumnMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/active_version/column_mapper.rb

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) ⇒ Object

Get column name for a concept



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) ⇒ Object

Get all mappings for a model and version type



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) ⇒ Object

Register a column mapping for a model and version type



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