Compliance SourceComplianceLoader

class Loader

Load compliance data from JSON files.

Nested

Definitions

def self.default(roots = [Dir.pwd])

Setup the default loader.

Implementation

def self.default(roots = [Dir.pwd])
	::Gem.loaded_specs.each do |name, spec|
		if path = spec.full_gem_path and File.directory?(path)
			compliance_path = File.expand_path("compliance.json", path)
			compliance_directory = File.expand_path("compliance", path)
			
			if File.file?(compliance_path) or File.directory?(compliance_directory)
				roots << path
			end
		end
	end
	
	roots.uniq!
	
	self.new(roots)
end

attr :roots

List of root directories to search for compliance documents.

def cache

Cache of name to path mappings.

Implementation

def cache
	@cache ||= build_cache
end

def resolve(name)

Map a name to a path.

Implementation

def resolve(name)
	cache[name]
end

def import(name, policy)

Import a named document into the policy.

Implementation

def import(name, policy)
	if path = cache[name]
		begin
			document = Document.load(path)
		rescue => error
			raise Error, "Error loading compliance document: #{path}!"
		end
		
		begin
			policy.add(document, self)
		rescue => error
			raise Error, "Error adding compliance document to policy: #{path}!"
		end
	else
		raise Error, "Could not find import: #{name}"
	end
end

def documents

Load all top level documents.

Implementation

def documents
	@roots.filter_map do |path|
		compliance_path = File.expand_path("compliance.json", path)
		if File.file?(compliance_path)
			begin
				Document.load(compliance_path)
			rescue => error
				raise Error, "Error loading compliance document: #{compliance_path}!"
			end
		end
	end
end