Class: RSMP::SXL::Processing::Catalogue

Inherits:
Object
  • Object
show all
Defined in:
lib/rsmp/sxl/processing/catalogue.rb

Overview

Indexes locally available SXL definitions by name and exact version.

Instance Method Summary collapse

Constructor Details

#initialize(paths = []) ⇒ Catalogue



6
7
8
9
# File 'lib/rsmp/sxl/processing/catalogue.rb', line 6

def initialize(paths = [])
  @documents = Hash.new { |hash, key| hash[key] = {} }
  Array(paths).each { |path| add_path(path) }
end

Instance Method Details

#add_document(document) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/rsmp/sxl/processing/catalogue.rb', line 27

def add_document(document)
  existing = @documents[document.name][document.version_string]
  if existing && existing.path != document.path
    raise Error,
          "Duplicate SXL #{document.name} #{document.version_string} in #{existing.path} and #{document.path}"
  end

  @documents[document.name][document.version_string] = document
  document
end

#add_path(path) ⇒ Object

Raises:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rsmp/sxl/processing/catalogue.rb', line 11

def add_path(path)
  expanded_path = File.expand_path(path)
  raise Error, "SXL source #{path} not found" unless File.exist?(expanded_path)

  if File.directory?(expanded_path)
    pattern = File.join(expanded_path, '**', '{sxl.yaml,sxl.yml}')
    files = Dir.glob(pattern)
    raise Error, "No sxl.yaml or sxl.yml files found under #{path}" if files.empty?

    files.each { |file| add_file(file) }
  else
    add_file(expanded_path)
  end
  self
end

#candidates(name, requirements = []) ⇒ Object



38
39
40
41
42
43
# File 'lib/rsmp/sxl/processing/catalogue.rb', line 38

def candidates(name, requirements = [])
  versions = @documents.fetch(name, {}).values.sort_by(&:version).reverse
  versions.select do |document|
    requirements.all? { |requirement| requirement.satisfied_by?(document.version) }
  end
end

#fetch(name, version) ⇒ Object



49
50
51
# File 'lib/rsmp/sxl/processing/catalogue.rb', line 49

def fetch(name, version)
  @documents.fetch(name, {})[version.to_s]
end

#versions(name) ⇒ Object



45
46
47
# File 'lib/rsmp/sxl/processing/catalogue.rb', line 45

def versions(name)
  @documents.fetch(name, {}).values.map(&:version_string).sort_by { |version| Gem::Version.new(version) }
end