class Promethee::Data::Localization

Public Class Methods

new(data, master_data = nil) click to toggle source
Calls superclass method Promethee::Data::new
# File lib/promethee/data/localization.rb, line 2
def initialize(data, master_data = nil)
  super data
  if master_data
    @master_data = Master.new master_data
    merge
  end
end

Protected Instance Methods

find_localized_component(id) click to toggle source
# File lib/promethee/data/localization.rb, line 62
def find_localized_component(id)
  return unless @data_before_merge
  return unless @data_before_merge.include? :components
  @data_before_merge[:components].find { |component| component[:id] == id }
end
get_component_without_attributes_values(component) click to toggle source
# File lib/promethee/data/localization.rb, line 36
def get_component_without_attributes_values(component)
  clean_component = component.deep_dup
  clean_component[:attributes] = clean_component[:attributes].keep_if { |key, object_value|
    object_value[:translatable]
  }.map { |key, object_value|
    [key, object_value.merge({ value: '' })]
  }.to_h if clean_component.has_key? :attributes

  clean_component
end
get_merged_translatable_component(master_component, localized_component) click to toggle source
# File lib/promethee/data/localization.rb, line 47
def get_merged_translatable_component(master_component, localized_component)
  # Not found in translation, we use the prepared master component
  return master_component if localized_component.nil?

  master_component[:attributes] ||= {}
  localized_component[:attributes] ||= {}

  # Merge values with the prepared master, allowing to add missing attributes from translation
  master_component[:attributes].each do |attr_key, attr_value|
    attr_value[:value] = localized_component.dig(:attributes, attr_key, :value) || ''
  end

  master_component
end
merge() click to toggle source

We want to be up to date with the master, so this method will: 1.take the master's components flattened 2 substitute values of what's already localized, based on the component id Consequences of step 1 are: 1 it adds new components from the master 2 it removes components not in the master anymore 3 it takes the order from the master 4 use all translatable attributes from the master 5 merge them with translation values if they exist

# File lib/promethee/data/localization.rb, line 21
def merge
  @data_before_merge = @data.deep_dup
  @data = {
    version: @master_data[:version],
    components: []
  }
  @master_data.flat.each do |master_component|
    translatable_master_component = get_component_without_attributes_values(master_component)
    localized_component = find_localized_component master_component[:id]

    component = get_merged_translatable_component(translatable_master_component, localized_component)
    @data[:components] << component
  end
end