Class: ROM::CommandCompiler Private

Inherits:
Object
  • Object
show all
Extended by:
Initializer
Defined in:
lib/rom/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

Instance Method Summary collapse

Instance Attribute Details

#cacheCache (readonly)

Returns local cache instance.

Returns:

  • (Cache)

    local cache instance



41
# File 'lib/rom/command_compiler.rb', line 41

option :cache, default: -> { Cache.new }

#command_classSymbol (readonly)

Returns The command command_class.

Returns:

  • (Symbol)

    The command command_class



25
# File 'lib/rom/command_compiler.rb', line 25

option :command_class, optional: true

#idSymbol (readonly)

Returns The command registry identifier.

Returns:

  • (Symbol)

    The command registry identifier



21
# File 'lib/rom/command_compiler.rb', line 21

option :id, optional: true

#metaArray<Symbol> (readonly)

Returns Meta data for a command.

Returns:

  • (Array<Symbol>)

    Meta data for a command



37
# File 'lib/rom/command_compiler.rb', line 37

option :meta, optional: true

#pluginsArray<Symbol> (readonly)

Returns a list of optional plugins that will be enabled for commands.

Returns:

  • (Array<Symbol>)

    a list of optional plugins that will be enabled for commands



33
# File 'lib/rom/command_compiler.rb', line 33

option :plugins, optional: true, default: -> { EMPTY_HASH }

#registryRegistry::Root (readonly)

Returns:

  • (Registry::Root)


29
# File 'lib/rom/command_compiler.rb', line 29

option :registry, default: -> { Registry::Root.new }

Instance Method Details

#[](id, adapter, ast, plugins, meta) ⇒ Command, CommandProxy Also known as: []

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 command_class 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:

  • id (Symbol)

    The command identifier

  • 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

  • meta (Hash)

    Meta data for a command

Returns:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rom/command_compiler.rb', line 63

def call(*args)
  cache.fetch_or_store(args.hash) do
    id, adapter, ast, plugins, plugins_options, meta = args

    component = registry.components.get(:commands, id: id)

    command_class =
      if component
        component.constant
      else
        Command.adapter_namespace(adapter).const_get(Inflector.classify(id))
      end

    plugins_with_opts = Array(plugins)
      .map { |plugin| [plugin, plugins_options.fetch(plugin) { EMPTY_HASH }] }
      .to_h

    compiler = with(
      id: id,
      command_class: command_class,
      adapter: adapter,
      plugins: plugins_with_opts,
      meta: meta
    )

    graph_opts = compiler.visit(ast)
    command = ROM::Commands::Graph.build(registry.root.commands, graph_opts)

    if command.graph?
      root = Inflector.singularize(command.name.relation).to_sym
      CommandProxy.new(command, root)
    elsif command.lazy?
      command.unwrap
    else
      command
    end
  end
end

#relationsObject

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.



110
111
112
# File 'lib/rom/command_compiler.rb', line 110

def relations
  registry.root.relations
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.



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

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