module GlobalID::Locator
Public Class Methods
locate(gid, options = {})
click to toggle source
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.
# File lib/global_id/locator.rb, line 12 def locate(gid, options = {}) if gid = GlobalID.parse(gid) locator_for(gid).locate gid if find_allowed?(gid.model_class, options[:only]) end end
use(app, locator = nil, &locator_block)
click to toggle source
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
# File lib/global_id/locator.rb, line 38 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
Private Class Methods
default_locator()
click to toggle source
# File lib/global_id/locator.rb, line 69 def self.default_locator ActiveRecordFinder.new end
find_allowed?(model_class, only = nil)
click to toggle source
# File lib/global_id/locator.rb, line 51 def find_allowed?(model_class, only = nil) only ? Array(only).any? { |c| model_class <= c } : true end
locator_for(gid)
click to toggle source
# File lib/global_id/locator.rb, line 47 def locator_for(gid) @locators.fetch(normalize_app(gid.app)) { default_locator } end
normalize_app(app)
click to toggle source
# File lib/global_id/locator.rb, line 55 def normalize_app(app) app.to_s.downcase end