Class: LogicaCompiler::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/logica_compiler/registry.rb

Defined Under Namespace

Classes: InvalidManifestError, MissingManifest, MissingManifestError, MissingQueryError, Unavailable

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manifest_path:, output_dir:) ⇒ Registry

Returns a new instance of Registry.



25
26
27
28
29
30
# File 'lib/logica_compiler/registry.rb', line 25

def initialize(manifest_path:, output_dir:)
  @manifest_path = manifest_path.to_s
  @output_dir = output_dir.to_s
  @entries = nil
  @sql_cache = {}
end

Class Method Details

.load(manifest_path:, output_dir:, strict: false) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/logica_compiler/registry.rb', line 15

def self.load(manifest_path:, output_dir:, strict: false)
  new(manifest_path:, output_dir:).tap { _1.load!(strict:) }
rescue Errno::ENOENT, MissingManifestError, InvalidManifestError => e
  raise if strict

  return unavailable(message: "#{e.message}. Run `bin/logica compile` again.") if e.is_a?(InvalidManifestError)

  MissingManifest.new(manifest_path:)
end

.unavailable(message:) ⇒ Object



11
12
13
# File 'lib/logica_compiler/registry.rb', line 11

def self.unavailable(message:)
  Unavailable.new(message: message.to_s)
end

Instance Method Details

#entry(name) ⇒ Object



57
58
59
# File 'lib/logica_compiler/registry.rb', line 57

def entry(name)
  entries.fetch(name.to_s) { raise MissingQueryError, "Unknown Logica query: #{name}" }
end

#load!(strict: false) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/logica_compiler/registry.rb', line 32

def load!(strict: false)
  data = JSON.parse(File.read(@manifest_path))
  queries = data.is_a?(Hash) ? data["queries"] : nil

  unless queries.is_a?(Hash)
    raise InvalidManifestError, "Invalid Logica manifest #{@manifest_path}: missing or invalid 'queries' key"
  end

  @entries = queries
  self
rescue Errno::ENOENT
  raise MissingManifestError, "Missing Logica manifest: #{@manifest_path}" if strict

  raise
rescue JSON::ParserError => e
  raise InvalidManifestError, "Invalid Logica manifest #{@manifest_path}: #{e.message}"
end

#sql(name) ⇒ Object



50
51
52
53
54
55
# File 'lib/logica_compiler/registry.rb', line 50

def sql(name)
  name = name.to_s
  entry = entries.fetch(name) { raise MissingQueryError, "Unknown Logica query: #{name}" }
  sql_file = entry.fetch("sql")
  @sql_cache[name] ||= File.read(File.join(@output_dir, sql_file))
end