Sha256: f1eaab054e068fb6ff059b943b7c467f1a63940862b3b702943c0c20d755c0d4

Contents?: true

Size: 1.31 KB

Versions: 17

Compression:

Stored size: 1.31 KB

Contents

class Promethee::Data::Localization < Promethee::Data
  def initialize(data, master_data = nil)
    super data
    if master_data
      @master_data = Master.new master_data
      merge
    end
  end

  protected

  # We want to be up to date with the master, so this method will:
  # 1.take the master's components flattened
  # 2 substitute 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
  def merge
    @data_before_merge = @data.deep_dup
    @data = {
      version: @master_data[:version],
      components: []
    }
    @master_data.flat.each do |master_component|
      localized_component = find_localized_component master_component[:id]
      # We take the localized component if it exists, the master component otherwise
      component = localized_component || master_component.except(:attributes)
      component[:attributes] ||= {}
      # We add it to the list of localized components
      @data[:components] << component
    end
  end

  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
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
promethee-3.0.2 lib/promethee/data/localization.rb
promethee-3.0.1 lib/promethee/data/localization.rb
promethee-3.0.0 lib/promethee/data/localization.rb
promethee-2.1.3 lib/promethee/data/localization.rb
promethee-2.1.2 lib/promethee/data/localization.rb
promethee-2.1.1 lib/promethee/data/localization.rb
promethee-2.1.0 lib/promethee/data/localization.rb
promethee-2.0.9 lib/promethee/data/localization.rb
promethee-2.0.8 lib/promethee/data/localization.rb
promethee-2.0.7 lib/promethee/data/localization.rb
promethee-2.0.6 lib/promethee/data/localization.rb
promethee-2.0.5 lib/promethee/data/localization.rb
promethee-2.0.4 lib/promethee/data/localization.rb
promethee-2.0.3 lib/promethee/data/localization.rb
promethee-2.0.2 lib/promethee/data/localization.rb
promethee-2.0.1 lib/promethee/data/localization.rb
promethee-2.0.0 lib/promethee/data/localization.rb