Class: LogicaCompiler::Config

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

Defined Under Namespace

Classes: Query

Constant Summary collapse

DEFAULT_ENGINE =
"postgres"
DEFAULT_OUTPUT_DIR =
"logica/compiled"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_path:, root:) ⇒ Config

Returns a new instance of Config.



33
34
35
36
37
38
39
40
# File 'lib/logica_compiler/config.rb', line 33

def initialize(config_path:, root:)
  @config_path = config_path.to_s
  @root = Pathname(root)
  @engine = DEFAULT_ENGINE
  @output_dir = DEFAULT_OUTPUT_DIR
  @queries = {}
  @logica_bin = env_logica_bin
end

Instance Attribute Details

#config_pathObject (readonly)

Returns the value of attribute config_path.



19
20
21
# File 'lib/logica_compiler/config.rb', line 19

def config_path
  @config_path
end

#engineObject (readonly)

Returns the value of attribute engine.



19
20
21
# File 'lib/logica_compiler/config.rb', line 19

def engine
  @engine
end

#logica_binObject (readonly)

Returns the value of attribute logica_bin.



19
20
21
# File 'lib/logica_compiler/config.rb', line 19

def logica_bin
  @logica_bin
end

#output_dirObject (readonly)

Returns the value of attribute output_dir.



19
20
21
# File 'lib/logica_compiler/config.rb', line 19

def output_dir
  @output_dir
end

#queriesObject (readonly)

Returns the value of attribute queries.



19
20
21
# File 'lib/logica_compiler/config.rb', line 19

def queries
  @queries
end

#rootObject (readonly)

Returns the value of attribute root.



19
20
21
# File 'lib/logica_compiler/config.rb', line 19

def root
  @root
end

Class Method Details

.default_rootObject



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

def self.default_root
  if defined?(Rails) && Rails.respond_to?(:root)
    Rails.root
  else
    Pathname.new(Dir.pwd)
  end
end

.load!(config_path = "logica/config.yml", root: default_root) ⇒ Object



21
22
23
# File 'lib/logica_compiler/config.rb', line 21

def self.load!(config_path = "logica/config.yml", root: default_root)
  new(config_path:, root:).tap(&:load!)
end

Instance Method Details

#absolute_path(rel_path) ⇒ Object



64
# File 'lib/logica_compiler/config.rb', line 64

def absolute_path(rel_path) = root.join(rel_path).to_s

#load!Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/logica_compiler/config.rb', line 42

def load!
  data = YAML.safe_load(File.read(absolute_path(config_path)), permitted_classes: [], aliases: false) || {}

  @engine = (data["engine"] || DEFAULT_ENGINE).to_s
  @output_dir = (data["output_dir"] || DEFAULT_OUTPUT_DIR).to_s
  @logica_bin ||= (data["logica_bin"] || default_logica_bin).to_s

  queries = (data["queries"] || {}).to_h
  @queries = queries.each_with_object({}) do |(name, attrs), h|
    attrs = attrs.to_h
    h[name.to_sym] = Query.new(
      program: attrs.fetch("program"),
      predicate: attrs.fetch("predicate")
    )
  end

  self
end

#manifest_pathObject



62
# File 'lib/logica_compiler/config.rb', line 62

def manifest_path = output_dir_path.join("manifest.json")

#output_dir_pathObject



61
# File 'lib/logica_compiler/config.rb', line 61

def output_dir_path = root.join(output_dir)

#pinned_logica_versionObject



66
67
68
69
70
71
72
73
74
# File 'lib/logica_compiler/config.rb', line 66

def pinned_logica_version
  req = requirements_path
  return nil unless req.exist?

  line = req.read.lines.map(&:strip).find { _1.start_with?("logica==") }
  return nil unless line

  line.split("==", 2).last&.strip
end

#requirements_pathObject



63
# File 'lib/logica_compiler/config.rb', line 63

def requirements_path = root.join("logica/requirements.txt")