class Lacerda::Infrastructure

Attributes

data_dir[R]
errors[R]

Public Class Methods

new(options) click to toggle source
# File lib/lacerda/infrastructure.rb, line 8
def initialize(options)
  @verbose = !!options.fetch(:verbose, false)
  @data_dir = options.fetch(:data_dir)
  @mutex1 = Mutex.new
  @mutex2 = Mutex.new
end

Public Instance Methods

consumers() click to toggle source
# File lib/lacerda/infrastructure.rb, line 73
def consumers
  services.values.select do |service|
    service.consumed_objects.length > 0
  end
end
contracts_fulfilled?(reporter = nil) click to toggle source
# File lib/lacerda/infrastructure.rb, line 19
def contracts_fulfilled?(reporter = nil)
  reporter = Lacerda.validate_reporter(reporter)

  @mutex1.synchronize do
    @errors = {}

    # Check for incompatibility in published objects
    reporter.try(:check_publishing)
    publishers.each do |publisher|
      reporter.try(:check_publisher, publisher)
      publisher.satisfies_consumers?(verbose: @verbose, reporter: reporter)
      next if publisher.errors.empty?
      @errors.merge! publisher.errors
    end

    # Check for missing publishers
    reporter.try(:check_consuming)
    missing_publishers = {}
    consumers.each do |consumer|
      reporter.try(:check_consumer, consumer)
      consumer.consumed_objects.each do |object|
        publisher_exists = !object.publisher.nil?
        is_published = publisher_exists && object.publisher.publishes?(object.name)
        reporter.try(:check_consumed_object, object.name, object.publisher_name.camelize, publisher_exists, is_published)
        if publisher_exists
          next
        else
          missing_publishers[object.publisher_name.camelize] ||= []
          missing_publishers[object.publisher_name.camelize] << consumer.name.camelize
        end
      end
    end

    # Report missing publishers
    unless missing_publishers.empty?
      missing = []
      missing_publishers.each do |publisher, consumers|
        missing << "#{publisher} (consumed by #{consumers.join(', ')})"
      end
      errors["Missing publishers"] = missing
    end

    reporter.try(:result, @errors)

    @errors.empty?
  end
end
convert_all!(keep_intermediary_files = false) click to toggle source
# File lib/lacerda/infrastructure.rb, line 79
def convert_all!(keep_intermediary_files = false)
  json_files.each{ |file| FileUtils.rm_f(file) }
  mson_files.each do |file|
    Lacerda::Conversion.mson_to_json_schema!(
      filename: file,
      keep_intermediary_files: keep_intermediary_files,
      verbose: @verbose)
  end
  reload
end
json_files() click to toggle source
# File lib/lacerda/infrastructure.rb, line 94
def json_files
  Dir.glob(File.join(@data_dir, "/**/*.schema.json"))
end
mson_files() click to toggle source
# File lib/lacerda/infrastructure.rb, line 90
def mson_files
  Dir.glob(File.join(@data_dir, "/**/*.mson"))
end
publishers() click to toggle source
# File lib/lacerda/infrastructure.rb, line 67
def publishers
  services.values.select do |service|
    service.published_objects.length > 0
  end
end
reload() click to toggle source
# File lib/lacerda/infrastructure.rb, line 15
def reload
  @services = nil
end
services() click to toggle source
# File lib/lacerda/infrastructure.rb, line 98
def services
  @mutex2.synchronize do
    return @services if @services
    @services = {}.with_indifferent_access
    dirs = Dir.glob(File.join(@data_dir, "*/"))
    dirs.each do |dir|
      service = Lacerda::Service.new(self, dir)
      @services[service.name] = service
    end
    @services
  end
end