Class: ROM::Repository::CommandCompiler Private

Inherits:
Object
  • Object
show all
Extended by:
Dry::Core::Cache
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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, adapter, container, registry, plugins, options) ⇒ 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
94
95
# File 'lib/rom/repository/command_compiler.rb', line 88

def initialize(id, adapter, container, registry, plugins, options)
  @id = id
  @adapter = adapter
  @registry = registry
  @container = container
  @plugins = Array(plugins)
  @options = options
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.



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

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.



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

def container
  @container
end

#idObject (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.



65
66
67
# File 'lib/rom/repository/command_compiler.rb', line 65

def id
  @id
end

#optionsObject (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 options
  @options
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.



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

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.



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

def registry
  @registry
end

Class Method Details

.[](container, type, adapter, ast, plugins, options) ⇒ 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:



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

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

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

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

    if command.graph?
      CommandProxy.new(command)
    elsif command.lazy?
      command.unwrap
    else
      command
    end
  end
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.



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

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

Instance Method Details

#typeObject

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.



98
99
100
101
102
# File 'lib/rom/repository/command_compiler.rb', line 98

def type
  @_type ||= Commands.const_get(Dry::Core::Inflector.classify(id))[adapter]
rescue NameError
  nil
end

#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.



105
106
107
108
# File 'lib/rom/repository/command_compiler.rb', line 105

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