Class: Acta::Upcaster::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/acta/upcaster.rb

Overview

Holds the merged set of ‘(event_type, from) → block` entries from every registered upcaster class. Also tracks the max `to` per event type so the pipeline can flag future-version records cleanly.

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



138
139
140
141
142
# File 'lib/acta/upcaster.rb', line 138

def initialize
  @by_key = {}
  @latest_to = Hash.new(0)
  @registered_classes = []
end

Instance Method Details

#clear!Object



176
177
178
179
180
# File 'lib/acta/upcaster.rb', line 176

def clear!
  @by_key.clear
  @latest_to.clear
  @registered_classes.clear
end

#empty?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/acta/upcaster.rb', line 172

def empty?
  @by_key.empty?
end

#find(event_type, from) ⇒ Object



164
165
166
# File 'lib/acta/upcaster.rb', line 164

def find(event_type, from)
  @by_key[[ event_type, from ]]
end

#latest_for(event_type) ⇒ Object



168
169
170
# File 'lib/acta/upcaster.rb', line 168

def latest_for(event_type)
  @latest_to[event_type]
end

#register(upcaster_class) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/acta/upcaster.rb', line 144

def register(upcaster_class)
  return if @registered_classes.include?(upcaster_class)

  upcaster_class.upcaster_registrations.each do |reg|
    key = [ reg[:event_type], reg[:from] ]
    if @by_key.key?(key)
      existing = @by_key[key]
      raise UpcasterRegistryError,
            "Conflicting upcasters for #{reg[:event_type].inspect} v#{reg[:from]}: " \
            "#{existing[:owner].name} already registered the (event_type, from) pair; " \
            "#{upcaster_class.name} tried to register it again."
    end

    @by_key[key] = reg.merge(owner: upcaster_class)
    @latest_to[reg[:event_type]] = [ @latest_to[reg[:event_type]], reg[:to] ].max
  end

  @registered_classes << upcaster_class
end