Sha256: a14c965c4aba24018c157239f2af553299f1d06b608e25091e0a76e540446457

Contents?: true

Size: 971 Bytes

Versions: 4

Compression:

Stored size: 971 Bytes

Contents

require 'versionomy'

module PactBroker
  module Domain
    class OrderVersions

      def self.call pacticipant_id
        orderable_versions = PactBroker::Domain::Version.where(:pacticipant_id => pacticipant_id).all.collect{| version | OrderableVersion.new(version) }
        orderable_versions.sort.each_with_index{ | version, i | version.update_model(i) }
      end

      class OrderableVersion

        attr_accessor :version_model, :sortable_number

        def initialize version_model
          @version_model = version_model
          @sortable_number = Versionomy.parse(version_model.number)
        end

        def <=> other
          self.sortable_number <=> other.sortable_number
        end

        def update_model new_order
          # Sequel will only run the update if the column value has changed, so in 99% of
          # cases, only one update will occur.
          version_model.update(:order => new_order)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pact_broker-1.6.0 lib/pact_broker/domain/order_versions.rb
pact_broker-1.5.0 lib/pact_broker/domain/order_versions.rb
pact_broker-1.4.0 lib/pact_broker/domain/order_versions.rb
pact_broker-1.3.2.rc1 lib/pact_broker/domain/order_versions.rb