Class: ElasticGraph::SchemaDefinition::Scripting::FileSystemRepository

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_graph/schema_definition/scripting/file_system_repository.rb

Overview

A simple abstraction that supports loading static scripts off of disk. The given directory is expected to have a sub-directory per script context, with individual scripts under the context sub-directories. The language is inferred from the script file extensions.

Constant Summary collapse

SUPPORTED_LANGUAGES_BY_EXTENSION =
{
  ".painless" => "painless",
  ".expression" => "expression",
  ".mustache" => "mustache",
  ".java" => "java"
}

Instance Method Summary collapse

Instance Method Details

#script_ids_by_scoped_nameObject

Map of script ids keyed by the ‘scoped_name` to allow easy lookup of the ids.



59
60
61
# File 'lib/elastic_graph/schema_definition/scripting/file_system_repository.rb', line 59

def script_ids_by_scoped_name
  @script_ids_by_scoped_name ||= scripts.to_h { |s| [s.scoped_name, s.id] }
end

#scriptsObject

The ‘Script` objects available in this file system repository.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/elastic_graph/schema_definition/scripting/file_system_repository.rb', line 33

def scripts
  @scripts ||= ::Pathname.new(dir).children.sort.flat_map do |context_dir|
    unless context_dir.directory?
      raise InvalidScriptDirectoryError, "`#{dir}` has a file (#{context_dir}) that is not a context directory as expected."
    end

    context_dir.children.sort.map do |script_file|
      unless script_file.file?
        raise InvalidScriptDirectoryError, "`#{dir}` has extra directory nesting (#{script_file}) that is unexpected."
      end

      language = SUPPORTED_LANGUAGES_BY_EXTENSION[script_file.extname] || raise(
        InvalidScriptDirectoryError, "`#{dir}` has a file (`#{script_file}`) that has an unrecognized file extension: #{script_file.extname}."
      )

      Script.new(
        name: script_file.basename.sub_ext("").to_s,
        source: script_file.read.strip,
        language: language,
        context: context_dir.basename.to_s
      )
    end
  end.tap { |all_scripts| verify_no_duplicates!(all_scripts) }
end