Class: ROM::Components::Registry

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rom/components/registry.rb

Constant Summary collapse

DUPLICATE_ERRORS =
{
  gateways: GatewayAlreadyDefinedError,
  datasets: DatasetAlreadyDefinedError,
  schemas: SchemaAlreadyDefinedError,
  relations: RelationAlreadyDefinedError,
  associations: AssociationAlreadyDefinedError,
  commands: CommandAlreadyDefinedError,
  mappers: MapperAlreadyDefinedError
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider:, handlers: ROM.components) ⇒ Registry

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



30
31
32
33
# File 'lib/rom/components/registry.rb', line 30

def initialize(provider:, handlers: ROM.components)
  @provider = provider
  @handlers = handlers
end

Instance Attribute Details

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



17
18
19
# File 'lib/rom/components/registry.rb', line 17

def handlers
  @handlers
end

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



14
15
16
# File 'lib/rom/components/registry.rb', line 14

def provider
  @provider
end

Instance Method Details

#[](type) ⇒ 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.



66
67
68
# File 'lib/rom/components/registry.rb', line 66

def [](type)
  store[type]
end

#add(type, item: nil, **options) ⇒ 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.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rom/components/registry.rb', line 76

def add(type, item: nil, **options)
  component = item || build(type, **options)

  # if include?(type, component)
  #   other = get(type, key: component.key)

  #   raise(
  #     DUPLICATE_ERRORS[type],
  #     "#{provider}: +#{component.key}+ is already defined by #{other.provider}"
  #   )
  # end

  store[type] << component

  update(component.local_components)

  component
end

#build(type, **options) ⇒ 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.



121
122
123
# File 'lib/rom/components/registry.rb', line 121

def build(type, **options)
  handlers[type].build(**options, provider: provider)
end

#call(key, &fallback) ⇒ 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.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rom/components/registry.rb', line 53

def call(key, &fallback)
  comp = detect { |_, component| component.key == key && !component.abstract? }&.last

  if comp
    comp.build
  elsif fallback
    fallback.()
  else
    raise KeyError, "+#{key}+ not found"
  end
end

#delete(type, item) ⇒ 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/components/registry.rb', line 104

def delete(type, item)
  self[type].delete(item)
  self
end

#eachObject

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.



41
42
43
44
45
# File 'lib/rom/components/registry.rb', line 41

def each
  store.each { |type, components|
    components.each { |component| yield(type, component) }
  }
end

#get(type, **opts) ⇒ 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.



71
72
73
# File 'lib/rom/components/registry.rb', line 71

def get(type, **opts)
  public_send(type, **opts).first
end

#include?(type, component) ⇒ Boolean

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:

  • (Boolean)


126
127
128
# File 'lib/rom/components/registry.rb', line 126

def include?(type, component)
  !component.abstract? && keys(type).include?(component.key)
end

#key?(key) ⇒ Boolean

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:

  • (Boolean)


131
132
133
# File 'lib/rom/components/registry.rb', line 131

def key?(key)
  keys.include?(key)
end

#keys(type = nil) ⇒ 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.



136
137
138
139
140
141
142
# File 'lib/rom/components/registry.rb', line 136

def keys(type = nil)
  if type
    self[type].map(&:key)
  else
    to_a.map(&:key)
  end
end

#replace(type, item: nil, **options) ⇒ 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
100
101
# File 'lib/rom/components/registry.rb', line 96

def replace(type, item: nil, **options)
  component = item || build(type, **options)
  delete(type, item) if include?(type, component)
  store[type] << component
  component
end

#storeObject

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.



36
37
38
# File 'lib/rom/components/registry.rb', line 36

def store
  @store ||= handlers.map { |handler| [handler.namespace, EMPTY_ARRAY.dup] }.to_h
end

#to_aObject

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.



48
49
50
# File 'lib/rom/components/registry.rb', line 48

def to_a
  flat_map { |_, components| components }
end

#update(other, **options) ⇒ 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.



110
111
112
113
114
115
116
117
118
# File 'lib/rom/components/registry.rb', line 110

def update(other, **options)
  other.each do |type, component|
    add(
      type,
      item: component.with(provider: provider, config: component.config.join(options, :right))
    )
  end
  self
end