Mange / roadie
1
# frozen_string_literal: true
2

3 5
module Roadie
4 5
  class StyleAttributeBuilder
5 5
    def initialize
6 5
      @styles = []
7
    end
8

9 5
    def <<(style)
10 5
      @styles << style
11
    end
12

13 5
    def attribute_string
14 5
      Deduplicator.apply(stable_sort(@styles).map(&:to_s)).join(';')
15
    end
16

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

Read our documentation on viewing source code .

Loading