Module: GlobalID::Locator
- Defined in:
- lib/global_id/locator.rb
Defined Under Namespace
Classes: BaseLocator, BlockLocator, UnscopedLocator
Class Method Summary collapse
-
.locate(gid, options = {}) ⇒ Object
Takes either a GlobalID or a string that can be turned into a GlobalID.
-
.locate_many(gids, options = {}) ⇒ Object
Takes an array of GlobalIDs or strings that can be turned into a GlobalIDs.
-
.locate_many_signed(sgids, options = {}) ⇒ Object
Takes an array of SignedGlobalIDs or strings that can be turned into a SignedGlobalIDs.
-
.locate_signed(sgid, options = {}) ⇒ Object
Takes either a SignedGlobalID or a string that can be turned into a SignedGlobalID.
-
.use(app, locator = nil, &locator_block) ⇒ Object
Tie a locator to an app.
Class Method Details
.locate(gid, options = {}) ⇒ Object
Takes either a GlobalID or a string that can be turned into a GlobalID
Options:
-
:only
- A class, module or Array of classes and/or modules that are allowed to be located. Passing one or more classes limits instances of returned classes to those classes or their subclasses. Passing one or more modules in limits instances of returned classes to those including that module. If no classes or modules match,nil
is returned.
14 15 16 17 18 |
# File 'lib/global_id/locator.rb', line 14 def locate(gid, = {}) if gid = GlobalID.parse(gid) locator_for(gid).locate gid if find_allowed?(gid.model_class, [:only]) end end |
.locate_many(gids, options = {}) ⇒ Object
Takes an array of GlobalIDs or strings that can be turned into a GlobalIDs. All GlobalIDs must belong to the same app, as they will be located using the same locator using its locate_many method.
By default the GlobalIDs will be located using Model.find(array_of_ids), so the models must respond to that finder signature.
This approach will efficiently call only one #find (or #where(id: id), when using ignore_missing) per model class, but still interpolate the results to match the order in which the gids were passed.
Options:
-
:only
- A class, module or Array of classes and/or modules that are allowed to be located. Passing one or more classes limits instances of returned classes to those classes or their subclasses. Passing one or more modules in limits instances of returned classes to those including that module. If no classes or modules match,nil
is returned. -
:ignore_missing
- By default, locate_many will call #find on the model to locate the ids extracted from the GIDs. In Active Record (and other data stores following the same pattern), #find will raise an exception if a named ID can’t be found. When you set this option to true, we will use #where(id: ids) instead, which does not raise on missing records.
40 41 42 43 44 45 46 47 |
# File 'lib/global_id/locator.rb', line 40 def locate_many(gids, = {}) if (allowed_gids = parse_allowed(gids, [:only])).any? locator = locator_for(allowed_gids.first) locator.locate_many(allowed_gids, ) else [] end end |
.locate_many_signed(sgids, options = {}) ⇒ Object
Takes an array of SignedGlobalIDs or strings that can be turned into a SignedGlobalIDs. The SignedGlobalIDs are located using Model.find(array_of_ids), so the models must respond to that finder signature.
This approach will efficiently call only one #find per model class, but still interpolate the results to match the order in which the gids were passed.
Options:
-
:only
- A class, module or Array of classes and/or modules that are allowed to be located. Passing one or more classes limits instances of returned classes to those classes or their subclasses. Passing one or more modules in limits instances of returned classes to those including that module. If no classes or modules match,nil
is returned.
74 75 76 |
# File 'lib/global_id/locator.rb', line 74 def locate_many_signed(sgids, = {}) locate_many sgids.collect { |sgid| SignedGlobalID.parse(sgid, .slice(:for)) }.compact, end |
.locate_signed(sgid, options = {}) ⇒ Object
Takes either a SignedGlobalID or a string that can be turned into a SignedGlobalID
Options:
-
:only
- A class, module or Array of classes and/or modules that are allowed to be located. Passing one or more classes limits instances of returned classes to those classes or their subclasses. Passing one or more modules in limits instances of returned classes to those including that module. If no classes or modules match,nil
is returned.
57 58 59 |
# File 'lib/global_id/locator.rb', line 57 def locate_signed(sgid, = {}) SignedGlobalID.find sgid, end |
.use(app, locator = nil, &locator_block) ⇒ Object
Tie a locator to an app. Useful when different apps collaborate and reference each others’ Global IDs.
The locator can be either a block or a class.
Using a block:
GlobalID::Locator.use :foo do |gid|
FooRemote.const_get(gid.model_name).find(gid.model_id)
end
Using a class:
GlobalID::Locator.use :bar, BarLocator.new
class BarLocator
def locate(gid)
@search_client.search name: gid.model_name, id: gid.model_id
end
end
98 99 100 101 102 103 104 |
# File 'lib/global_id/locator.rb', line 98 def use(app, locator = nil, &locator_block) raise ArgumentError, 'No locator provided. Pass a block or an object that responds to #locate.' unless locator || block_given? URI::GID.validate_app(app) @locators[normalize_app(app)] = locator || BlockLocator.new(locator_block) end |