Sha256: 6d8846641ab528282e388d15f946ef28c91db870d0a77e8ae3d0ef0797be8fb5

Contents?: true

Size: 662 Bytes

Versions: 8

Compression:

Stored size: 662 Bytes

Contents

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

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

    def attribute_string
      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

8 entries across 8 versions & 1 rubygems

Version Path
roadie-3.1.0 lib/roadie/style_attribute_builder.rb
roadie-3.1.0.rc1 lib/roadie/style_attribute_builder.rb
roadie-3.0.5 lib/roadie/style_attribute_builder.rb
roadie-3.0.4 lib/roadie/style_attribute_builder.rb
roadie-3.0.3 lib/roadie/style_attribute_builder.rb
roadie-3.0.2 lib/roadie/style_attribute_builder.rb
roadie-3.0.1 lib/roadie/style_attribute_builder.rb
roadie-3.0.0 lib/roadie/style_attribute_builder.rb