Sha256: b3c5b262d17a1ee82ce722bdd4f2827b20b480ea1c509d9aaa21b778801e2182

Contents?: true

Size: 713 Bytes

Versions: 1

Compression:

Stored size: 713 Bytes

Contents

# frozen_string_literal: true

module Roadie
  class StyleAttributeBuilder
    def initialize
      @styles = []
    end

    def <<(style)
      @styles << style
    end

    def attribute_string
      Deduplicator.apply(stable_sort(@styles).map(&:to_s)).join(';')
    end

    private
    def stable_sort(list)
      # Ruby's sort is unstable for performance reasons. We need it to be
      # stable, e.g. to preserve order of elements that are compared equal in
      # the sorting.
      # We can accomplish this by using the original array index as a second
      # comparator for when the first one is equal.
      list.each_with_index.sort_by { |item, index| [item, index] }.map(&:first)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
roadie-4.0.0 lib/roadie/style_attribute_builder.rb