Class: ZeroRailsAdapter::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/zero_rails_adapter/registry.rb

Constant Summary collapse

SEPARATOR =
/[|.]/

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



7
8
9
# File 'lib/zero_rails_adapter/registry.rb', line 7

def initialize
  @mutators = {}
end

Instance Method Details

#fetch(name) ⇒ Object



18
19
20
# File 'lib/zero_rails_adapter/registry.rb', line 18

def fetch(name)
  find(name) || raise(UnknownMutatorError, "Could not find mutator #{name}")
end

#find(name) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/zero_rails_adapter/registry.rb', line 22

def find(name)
  key = canonical(name)
  mutator = resolve(@mutators[key])
  return mutator if mutator

  conventional_class_name(name).safe_constantize
  mutator = resolve(@mutators[key])
  return mutator if mutator
end

#register(mutator_class, as: mutator_class.mutation_name) ⇒ Object

Raises:

  • (ArgumentError)


11
12
13
14
15
16
# File 'lib/zero_rails_adapter/registry.rb', line 11

def register(mutator_class, as: mutator_class.mutation_name)
  raise ArgumentError, "mutation_name must be configured" if as.to_s.empty?

  @mutators[canonical(as)] = mutator_class.name.to_s.empty? ? mutator_class : mutator_class.name
  mutator_class
end

#registered_namesObject



32
33
34
# File 'lib/zero_rails_adapter/registry.rb', line 32

def registered_names
  @mutators.keys.sort
end