Class: Spree::NumberGenerators::Registry
- Inherits:
-
Object
- Object
- Spree::NumberGenerators::Registry
- Defined in:
- lib/spree/number_generators/registry.rb
Overview
Maps a resource key (:order, :return, …) to the generator that
numbers it. Empty by default: with no explicit entry, the store's
document_number_format preference decides between the sequential and
random strategies, which is what makes the dashboard switch work
without any registration at all.
Spree.number_generators[:order] = 'MyApp::BranchOrderNumbers'
Spree.number_generators[:order] # => the class
Spree.number_generators.delete(:order) # back to store settings
Values are stored as strings and constantized on read so a host app can register in an initializer without eager-loading the class, and so Zeitwerk reloads resolve the current definition in development.
Constant Summary collapse
- FORMATS =
{ 'sequential' => 'Spree::NumberGenerators::Sequential', 'random' => 'Spree::NumberGenerators::Random' }.freeze
- DEFAULT_FORMAT =
'sequential'.freeze
Instance Method Summary collapse
-
#[](key) ⇒ Class?
The registered class, without falling back.
- #[]=(key, class_name) ⇒ Object
- #clear ⇒ Object
- #delete(key) ⇒ Object
-
#for(key, store: nil) ⇒ Spree::NumberGenerators::Base
The generator for a resource: an explicit registration if there is one, otherwise the strategy named by the format preference of the store the record belongs to.
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
- #keys ⇒ Object
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
24 25 26 |
# File 'lib/spree/number_generators/registry.rb', line 24 def initialize @overrides = {} end |
Instance Method Details
#[](key) ⇒ Class?
Returns the registered class, without falling back.
39 40 41 |
# File 'lib/spree/number_generators/registry.rb', line 39 def [](key) @overrides[key.to_sym]&.constantize end |
#[]=(key, class_name) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/spree/number_generators/registry.rb', line 30 def []=(key, class_name) if class_name.nil? delete(key) else @overrides[key.to_sym] = class_name.to_s end end |
#clear ⇒ Object
51 52 53 |
# File 'lib/spree/number_generators/registry.rb', line 51 def clear @overrides.clear end |
#delete(key) ⇒ Object
43 44 45 |
# File 'lib/spree/number_generators/registry.rb', line 43 def delete(key) @overrides.delete(key.to_sym) end |
#for(key, store: nil) ⇒ Spree::NumberGenerators::Base
The generator for a resource: an explicit registration if there is one, otherwise the strategy named by the format preference of the store the record belongs to.
65 66 67 68 69 70 |
# File 'lib/spree/number_generators/registry.rb', line 65 def for(key, store: nil) registered = self[key] return registered.new if registered format_class(store).new end |
#keys ⇒ Object
47 48 49 |
# File 'lib/spree/number_generators/registry.rb', line 47 def keys @overrides.keys end |