Class: Exid::Record
- Inherits:
-
Module
- Object
- Module
- Exid::Record
- Defined in:
- lib/exid/record.rb
Defined Under Namespace
Classes: Entry
Constant Summary collapse
- MUTEX =
Mutex.new
Class Method Summary collapse
- .fetch(eid) ⇒ Object
- .fetch!(eid) ⇒ Object
- .find_module(prefix) ⇒ Object
- .finder(eid) ⇒ Object
-
.register_module(entry) ⇒ Object
When app is eager loaded in production, all models are loaded and registered.
- .registered_modules ⇒ Object
- .unload ⇒ Object
Instance Method Summary collapse
- #included(base) ⇒ Object
-
#initialize(prefix, field) ⇒ Record
constructor
The prefix can be a single String or an Array of Strings.
Constructor Details
#initialize(prefix, field) ⇒ Record
The prefix can be a single String or an Array of Strings. The first prefix is the canonical one, used to generate external IDs. Any additional prefixes are treated as aliases and are only used to locate records. This allows renaming a prefix without breaking existing IDs.
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/exid/record.rb', line 38 def initialize(prefix, field) prefixes = Array(prefix) prefixes.each { |item| Exid.configuration.validate_prefix(item) } @module_static = build_module_static(prefixes, field) @module_value = build_module_value(prefixes.first, field) @module_shared = build_module_shared(prefixes, field) super() end |
Class Method Details
.fetch(eid) ⇒ Object
85 |
# File 'lib/exid/record.rb', line 85 def self.fetch(eid) = finder(eid).first |
.fetch!(eid) ⇒ Object
87 |
# File 'lib/exid/record.rb', line 87 def self.fetch!(eid) = finder(eid).sole |
.find_module(prefix) ⇒ Object
73 74 75 76 |
# File 'lib/exid/record.rb', line 73 def self.find_module(prefix) registered_modules.detect { _1.prefixes.include?(prefix) } or raise Error, "Model for \"#{prefix}\" not found" end |
.finder(eid) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/exid/record.rb', line 78 def self.finder(eid) Coder.decode(eid) => prefix, value mod = find_module(prefix) mod.klass.where(mod.field => value) end |
.register_module(entry) ⇒ Object
When app is eager loaded in production, all models are loaded and registered. This logic with delte and readd is for development purpose, to make sure we can make changes to the app and have this logic working at the same time. We only use prefix as identify for the set elements.
54 55 56 57 58 59 |
# File 'lib/exid/record.rb', line 54 def self.register_module(entry) MUTEX.synchronize do @registered_modules.delete(entry) @registered_modules.add(entry) end end |
.registered_modules ⇒ Object
67 68 69 70 71 |
# File 'lib/exid/record.rb', line 67 def self.registered_modules MUTEX.synchronize do @registered_modules.dup end end |
.unload ⇒ Object
61 62 63 64 65 |
# File 'lib/exid/record.rb', line 61 def self.unload MUTEX.synchronize do @registered_modules = Set.new end end |
Instance Method Details
#included(base) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/exid/record.rb', line 18 def included(base) base.public_send(:include, @module_value) base.public_send(:include, @module_shared) base.public_send(:extend, @module_shared) base.public_send(:extend, @module_static) self.class.register_module( Entry.new( prefixes: base.exid_prefix_names, field: base.exid_field, klass: base ) ) end |