Sha256: 7fd40bc0176c9113c53ad18df658fc6af1be406b1833d413879e0669f375a2dd

Contents?: true

Size: 1.7 KB

Versions: 21

Compression:

Stored size: 1.7 KB

Contents

module Pact
  module ActiveSupportSupport

    extend self

    def fix_all_the_things thing
      if thing.is_a?(Regexp)
        fix_regexp(thing)
      elsif thing.is_a?(Array)
        thing.each{ | it | fix_all_the_things it }
      elsif thing.is_a?(Hash)
          thing.values.each{ | it | fix_all_the_things it }
      elsif thing.class.name.start_with?("Pact")
        thing.instance_variables.collect{ | iv_name | thing.instance_variable_get(iv_name)}.each do | iv |
          fix_all_the_things iv
        end
      end
      thing
    end

    # ActiveSupport JSON overwrites (i.e. TRAMPLES) the json methods of the Regexp class directly
    # (beneath its destructive hooves of destruction).
    # This does not seem to be able to be undone without affecting the JSON serialisation in the
    # calling project, so the best way I've found to fix this issue is to reattach the
    # original as_json to the Regexp instances in the ConsumerContract before we write them to the
    # pact file. If anyone can find a better way, please submit a pull request ASAP!
    def fix_regexp regexp
      def regexp.as_json options = {}
        {:json_class => 'Regexp', "o" => self.options, "s" => self.source }
      end
      regexp
    end

    # Having Active Support JSON loaded somehow kills the formatting of pretty_generate for objects.
    # Don't ask me why, but it still seems to work for hashes, so the hacky work around is to
    # reparse the generated JSON into a hash and pretty_generate that... sigh...
    # Oh ActiveSupport, why....
    def fix_json_formatting json
      if json.include?("\n")
        json
      else
        JSON.pretty_generate(JSON.parse(json, create_additions: false))
      end
    end

  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
pact-1.1.0.rc2 lib/pact/consumer_contract/active_support_support.rb
pact-1.0.38 lib/pact/consumer_contract/active_support_support.rb
pact-1.1.0.rc1 lib/pact/consumer_contract/active_support_support.rb
pact-1.0.37 lib/pact/consumer_contract/active_support_support.rb
pact-1.0.36 lib/pact/consumer_contract/active_support_support.rb
pact-1.0.35 lib/pact/consumer_contract/active_support_support.rb
pact-1.0.34 lib/pact/consumer_contract/active_support_support.rb
pact-1.0.33 lib/pact/consumer_contract/active_support_support.rb
pact-1.0.32 lib/pact/consumer_contract/active_support_support.rb
pact-1.0.31 lib/pact/consumer_contract/active_support_support.rb
pact-1.0.30 lib/pact/consumer_contract/active_support_support.rb
pact-1.0.29 lib/pact/consumer_contract/active_support_support.rb
pact-1.0.28 lib/pact/consumer_contract/active_support_support.rb
pact-1.0.27 lib/pact/consumer_contract/active_support_support.rb
pact-1.0.26 lib/pact/consumer_contract/active_support_support.rb
pact-1.0.25 lib/pact/consumer_contract/active_support_support.rb
pact-1.0.24 lib/pact/consumer_contract/active_support_support.rb
pact-1.0.23 lib/pact/consumer_contract/active_support_support.rb
pact-1.0.22 lib/pact/consumer_contract/active_support_support.rb
pact-1.0.21 lib/pact/consumer_contract/active_support_support.rb