Module: Pact::ActiveSupportSupport
- Extended by:
- ActiveSupportSupport
- Included in:
- ActiveSupportSupport, DifferenceIndicator, Interaction, Matchers::EmbeddedDiffFormatter, QueryHash, QueryString, Reification, Term
- Defined in:
- lib/pact/shared/active_support_support.rb
Instance Method Summary collapse
- #fix_all_the_things(thing) ⇒ Object
-
#fix_json_formatting(json) ⇒ Object
Having Active Support JSON loaded somehow kills the formatting of pretty_generate for objects.
-
#fix_regexp(regexp) ⇒ Object
ActiveSupport JSON overwrites (i.e. TRAMPLES) the json methods of the Regexp class directly (beneath its destructive hooves of destruction).
- #remove_unicode(json) ⇒ Object
- #warn_about_regexp(thing) ⇒ Object
Instance Method Details
#fix_all_the_things(thing) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/pact/shared/active_support_support.rb', line 7 def fix_all_the_things thing if defined?(ActiveSupport) if thing.is_a?(Regexp) fix_regexp(thing) elsif thing.is_a?(Array) thing.collect{ | it | fix_all_the_things it } elsif thing.is_a?(Hash) thing.each_with_object({}) { | (k, v), new_hash | new_hash[k] = fix_all_the_things(v) } elsif thing.is_a?(Pact::Term) # matcher Regexp is fixed in its own as_json method thing elsif thing.class.name.start_with?("Pact") warn_about_regexp(thing) thing else thing end else thing end end |
#fix_json_formatting(json) ⇒ Object
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.…
43 44 45 46 47 48 49 50 |
# File 'lib/pact/shared/active_support_support.rb', line 43 def fix_json_formatting json if json =~ /\{".*?":"/ json = JSON.pretty_generate(JSON.parse(json, create_additions: false)) else json end fix_empty_hash_and_array json end |
#fix_regexp(regexp) ⇒ Object
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!
35 36 37 |
# File 'lib/pact/shared/active_support_support.rb', line 35 def fix_regexp regexp {:json_class => 'Regexp', "o" => regexp., "s" => regexp.source } end |
#remove_unicode(json) ⇒ Object
52 53 54 |
# File 'lib/pact/shared/active_support_support.rb', line 52 def remove_unicode json json.gsub(/\\u([0-9A-Za-z]{4})/) {|s| [$1.to_i(16)].pack("U")} end |
#warn_about_regexp(thing) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/pact/shared/active_support_support.rb', line 56 def warn_about_regexp(thing) thing.instance_variables.each do | iv_name | iv = thing.instance_variable_get(iv_name) if iv.is_a?(Regexp) require 'pact/configuration' Pact.configuration.error_stream.puts("WARN: Instance variable #{iv_name} for class #{thing.class.name} is a Regexp and isn't been serialized properly. Please raise an issue at https://github.com/pact-foundation/pact-support/issues/new.") end end end |