class Pact::Configuration

Constants

DEFAULT_DIFFER
DIFFERS
DIFF_FORMATTERS
DIFF_FORMATTER_REGISTRATIONS

Attributes

error_stream[RW]
log_dir[RW]
logger[W]
output_stream[RW]
pact_dir[RW]
pactfile_write_order[RW]
tmp_dir[RW]
treat_all_number_classes_as_equivalent[RW]

Public Class Methods

default_configuration() click to toggle source
# File lib/pact/configuration.rb, line 57
def self.default_configuration
  c = Configuration.new
  c.pact_dir = File.expand_path('./spec/pacts')
  c.tmp_dir = File.expand_path('./tmp/pacts')
  c.log_dir = default_log_dir

  c.output_stream = $stdout
  c.error_stream = $stderr
  c.pactfile_write_order = :chronological
  c.treat_all_number_classes_as_equivalent = true

  c
end
default_log_dir() click to toggle source
# File lib/pact/configuration.rb, line 163
def self.default_log_dir
  File.expand_path("./log")
end
new() click to toggle source
# File lib/pact/configuration.rb, line 71
def initialize
  @differ_registrations = []
  @diff_formatter_registrations = []
end

Public Instance Methods

body_differ_for_content_type(content_type) click to toggle source
# File lib/pact/configuration.rb, line 101
def body_differ_for_content_type content_type
  differ_registrations
    .find{ | registration | registration.first =~ content_type }
    .tap do |it|
      if content_type.nil? && it.last == Pact::TextDiffer
        error_stream.puts "WARN: No content type found, performing text diff on body"
        logger.warn "No content type found, performing text diff on body"
      end
    end.last
end
color_enabled() click to toggle source
# File lib/pact/configuration.rb, line 116
def color_enabled
  # Can't use ||= when the variable might be false, it will execute the expression if it's false
  color_enabled = defined?(@color_enabled) ? @color_enabled : true
  Rainbow.enabled = true if color_enabled
  color_enabled
end
color_enabled=(color_enabled) click to toggle source
# File lib/pact/configuration.rb, line 123
def color_enabled= color_enabled
  @color_enabled = color_enabled
end
diff_formatter=(diff_formatter) click to toggle source

Should this be deprecated in favour of register_diff_formatter???

# File lib/pact/configuration.rb, line 81
def diff_formatter= diff_formatter
  register_diff_formatter(/.*/, diff_formatter)
  register_diff_formatter(nil, diff_formatter)
end
diff_formatter_for_content_type(content_type) click to toggle source
# File lib/pact/configuration.rb, line 91
def diff_formatter_for_content_type content_type
  diff_formatter_registrations.find{ | registration | registration.first =~ content_type }.last
end
log_path() click to toggle source
# File lib/pact/configuration.rb, line 112
def log_path
  log_dir + "/pact.log"
end
logger() click to toggle source
# File lib/pact/configuration.rb, line 76
def logger
  @logger ||= create_logger
end
register_body_differ(content_type, differ) click to toggle source
# File lib/pact/configuration.rb, line 95
def register_body_differ content_type, differ
  key = content_type_regexp_for content_type
  validate_differ differ
  @differ_registrations << [key, differ]
end
register_diff_formatter(content_type, diff_formatter) click to toggle source
# File lib/pact/configuration.rb, line 86
def register_diff_formatter content_type, diff_formatter
  key = content_type_regexp_for content_type
  @diff_formatter_registrations << [key, diff_formatter_for(diff_formatter)]
end

Private Instance Methods

content_type_regexp_for(content_type) click to toggle source
# File lib/pact/configuration.rb, line 145
def content_type_regexp_for content_type
  case content_type
  when String then Regexp.new(/^#{Regexp.escape(content_type)}$/)
  when Regexp then content_type
  when nil then NilMatcher
  else
    raise "Invalid content type used to register a differ (#{content_type.inspect}). Please use a Regexp or a String."
  end
end
create_logger() click to toggle source
# File lib/pact/configuration.rb, line 172
def create_logger
  FileUtils::mkdir_p log_dir
  logger = ::Logger.new(log_path)
  logger.level = ::Logger::DEBUG
  logger
rescue Errno::EROFS
  # So we can run on RunKit
  logger = ::Logger.new($stdout)
  logger.level = ::Logger::DEBUG
  logger
end
diff_formatter_for(input) click to toggle source
# File lib/pact/configuration.rb, line 129
def diff_formatter_for input
  if DIFF_FORMATTERS[input]
    DIFF_FORMATTERS[input]
  elsif input.respond_to?(:call)
    input
  else
    raise "Pact diff_formatter needs to respond to call, or be in the preconfigured list: #{DIFF_FORMATTERS.keys}"
  end
end
diff_formatter_registrations() click to toggle source
# File lib/pact/configuration.rb, line 159
def diff_formatter_registrations
  @diff_formatter_registrations + DIFF_FORMATTER_REGISTRATIONS
end
differ_registrations() click to toggle source
# File lib/pact/configuration.rb, line 155
def differ_registrations
  @differ_registrations + DIFFERS
end
is_rake_running?() click to toggle source

Would love a better way of determining this! It sure won’t work on windows.

# File lib/pact/configuration.rb, line 168
def is_rake_running?
  `ps -ef | grep rake | grep #{Process.ppid} | grep -v 'grep'`.size > 0
end
validate_differ(differ) click to toggle source
# File lib/pact/configuration.rb, line 139
def validate_differ differ
  if !differ.respond_to?(:call)
    raise "Pact.configuration.register_body_differ expects a differ that is a lamda or a class/object that responds to call."
  end
end