Module: GlobalID::Locator

Defined in:
lib/global_id/locator.rb

Defined Under Namespace

Classes: ActiveRecordFinder, BlockLocator

Class Method Summary collapse

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.



12
13
14
15
16
# 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) ⇒ 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

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
# 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