Skip to content

Commit

Permalink
refactor: separate general object ordering code from pact specific or…
Browse files Browse the repository at this point in the history
…dering code
  • Loading branch information
bethesque committed Apr 25, 2019
1 parent e4d3c3a commit af0c51b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
25 changes: 25 additions & 0 deletions lib/pact_broker/pacts/order_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'pact_broker/json'

module PactBroker
module Pacts
class OrderObject
def self.call thing
case thing
when Hash then order_hash(thing)
when Array then order_child_array(thing)
else thing
end
end

def self.order_child_array array
array.collect{ |thing| call(thing) }
end

def self.order_hash hash
hash.keys.sort.each_with_object({}) do | key, new_hash |
new_hash[key] = call(hash[key])
end
end
end
end
end
20 changes: 3 additions & 17 deletions lib/pact_broker/pacts/sort_content.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'pact_broker/json'
require 'pact_broker/pacts/order_object'

module PactBroker
module Pacts
Expand Down Expand Up @@ -26,28 +27,13 @@ def self.verifiable_content_key_for pact_hash
end
end


def self.order_verifiable_content array
array_with_ordered_hashes = order_object(array)
array_with_ordered_hashes.sort{|a, b| a.to_json <=> b.to_json }
array_with_ordered_hashes.sort{ |a, b| a.to_json <=> b.to_json }
end

def self.order_object thing
case thing
when Hash then order_hash(thing)
when Array then order_child_array(thing)
else thing
end
end

def self.order_child_array array
array.collect{|thing| order_object(thing) }
end

def self.order_hash hash
hash.keys.sort.each_with_object({}) do | key, new_hash |
new_hash[key] = order_object(hash[key])
end
OrderObject.call(thing)
end
end
end
Expand Down

0 comments on commit af0c51b

Please sign in to comment.