Class: ROM::Repository::CommandCompiler Private

Inherits:
Object
  • Object
show all
Defined in:
lib/rom/repository/command_compiler.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Builds commands for relations.

This class is used by repositories to automatically create commands for their relations. This is used both by ‘Repository#command` method and `commands` repository class macros.

Constant Summary collapse

SUPPORTED_TYPES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%i[create update delete].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, adapter, container, registry, plugins) ⇒ CommandCompiler

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of CommandCompiler.



88
89
90
91
92
93
# File 'lib/rom/repository/command_compiler.rb', line 88

def initialize(type, adapter, container, registry, plugins)
  @type = Commands.const_get(Inflector.classify(type))[adapter]
  @registry = registry
  @container = container
  @plugins = Array(plugins)
end

Instance Attribute Details

#adapterObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



73
74
75
# File 'lib/rom/repository/command_compiler.rb', line 73

def adapter
  @adapter
end

#containerObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



77
78
79
# File 'lib/rom/repository/command_compiler.rb', line 77

def container
  @container
end

#pluginsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



85
86
87
# File 'lib/rom/repository/command_compiler.rb', line 85

def plugins
  @plugins
end

#registryObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



81
82
83
# File 'lib/rom/repository/command_compiler.rb', line 81

def registry
  @registry
end

#typeObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



69
70
71
# File 'lib/rom/repository/command_compiler.rb', line 69

def type
  @type
end

Class Method Details

.[](*args) ⇒ Command, CommandProxy

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return a specific command type for a given adapter and relation AST

This class holds its own registry where all generated commands are being stored

CommandProxy is returned for complex command graphs as they expect root relation name to be present in the input, which we don’t want to have in repositories. It might be worth looking into removing this requirement from rom core Command::Graph API.

Parameters:

  • container (ROM::Container)

    container where relations are stored

  • type (Symbol)

    The type of command

  • adapter (Symbol)

    The adapter identifier

  • ast (Array)

    The AST representation of a relation

  • plugins (Array<Symbol>)

    A list of optional command plugins that should be used

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rom/repository/command_compiler.rb', line 37

def self.[](*args)
  cache.fetch_or_store(args.hash) do
    container, type, adapter, ast, plugins = args

    unless SUPPORTED_TYPES.include?(type)
      raise ArgumentError, "#{type.inspect} is not a supported command type"
    end

    graph_opts = new(type, adapter, container, registry, plugins).visit(ast)

    command = ROM::Commands::Graph.build(registry, graph_opts)

    if command.graph?
      CommandProxy.new(command)
    else
      command.unwrap
    end
  end
end

.cacheObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



58
59
60
# File 'lib/rom/repository/command_compiler.rb', line 58

def self.cache
  @__cache__ ||= Concurrent::Map.new
end

.registryObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



63
64
65
# File 'lib/rom/repository/command_compiler.rb', line 63

def self.registry
  @__registry__ ||= Hash.new { |h, k| h[k] = {} }
end

Instance Method Details

#visit(ast, *args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



96
97
98
99
# File 'lib/rom/repository/command_compiler.rb', line 96

def visit(ast, *args)
  name, node = ast
  __send__(:"visit_#{name}", node, *args)
end