module GlobalID::Locator
def find_allowed?(model_class, only = nil)
def find_allowed?(model_class, only = nil) only ? Array(only).any? { |c| model_class <= c } : true end
def locate(gid, options = {})
instances of returned classes to those including that module. If no classes or
classes to those classes or their subclasses. Passing one or more modules in limits
allowed to be located. Passing one or more classes limits instances of returned
* :only - A class, module or Array of classes and/or modules that are
Options:
Takes either a GlobalID or a string that can be turned into a GlobalID
def locate(gid, options = {}) if gid = GlobalID.parse(gid) locator_for(gid).locate gid if find_allowed?(gid.model_class, options[:only]) end end
def locate_signed(sgid, options = {})
instances of returned classes to those including that module. If no classes or
classes to those classes or their subclasses. Passing one or more modules in limits
allowed to be located. Passing one or more classes limits instances of returned
* :only - A class, module or Array of classes and/or modules that are
Options:
Takes either a SignedGlobalID or a string that can be turned into a SignedGlobalID
def locate_signed(sgid, options = {}) SignedGlobalID.find sgid, options end
def locator_for(gid)
def locator_for(gid) @locators.fetch(normalize_app(gid.app)) { default_locator } end
def normalize_app(app)
def normalize_app(app) app.to_s.downcase end
def use(app, locator = nil, &locator_block)
end
@search_client.search name: gid.model_name, id: gid.model_id
def locate(gid)
class BarLocator
GlobalID::Locator.use :bar, BarLocator.new
Using a class:
end
FooRemote.const_get(gid.model_name).find(gid.model_id)
GlobalID::Locator.use :foo do |gid|
Using a block:
The locator can be either a block or a class.
Useful when different apps collaborate and reference each others' Global IDs.
Tie a locator to an app.
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? GlobalID.validate_app(app) @locators[normalize_app(app)] = locator || BlockLocator.new(locator_block) end