class GlobalID

Constants

VERSION

Attributes

app[R]
uri[R]

Public Class Methods

app=(app) click to toggle source
# File lib/global_id/global_id.rb, line 32
def app=(app)
  @app = URI::GID.validate_app(app)
end
create(model, options = {}) click to toggle source
# File lib/global_id/global_id.rb, line 12
def create(model, options = {})
  if app = options.fetch(:app) { GlobalID.app }
    params = options.except(:app, :verifier, :for)
    new URI::GID.create(app, model, params), options
  else
    raise ArgumentError, 'An app is required to create a GlobalID. ' \
      'Pass the :app option or set the default GlobalID.app.'
  end
end
default_locator(default_locator) click to toggle source
# File lib/global_id/global_id.rb, line 36
def default_locator(default_locator)
  Locator.default_locator = default_locator
end
eager_load!() click to toggle source
Calls superclass method
# File lib/global_id.rb, line 17
def self.eager_load!
  super
  require 'global_id/signed_global_id'
end
find(gid, options = {}) click to toggle source
# File lib/global_id/global_id.rb, line 22
def find(gid, options = {})
  parse(gid, options).try(:find, options)
end
new(gid, options = {}) click to toggle source
# File lib/global_id/global_id.rb, line 49
def initialize(gid, options = {})
  @uri = gid.is_a?(URI::GID) ? gid : URI::GID.parse(gid)
end
parse(gid, options = {}) click to toggle source
# File lib/global_id/global_id.rb, line 26
def parse(gid, options = {})
  gid.is_a?(self) ? gid : new(gid, options)
rescue URI::Error
  parse_encoded_gid(gid, options)
end

Private Class Methods

parse_encoded_gid(gid, options) click to toggle source
# File lib/global_id/global_id.rb, line 41
def parse_encoded_gid(gid, options)
  new(Base64.urlsafe_decode64(gid), options) rescue nil
end

Public Instance Methods

==(other) click to toggle source
# File lib/global_id/global_id.rb, line 81
def ==(other)
  other.is_a?(GlobalID) && @uri == other.uri
end
Also aliased as: eql?
as_json(*) click to toggle source
# File lib/global_id/global_id.rb, line 94
def as_json(*)
  to_s
end
eql?(other)
Alias for: ==
find(options = {}) click to toggle source
# File lib/global_id/global_id.rb, line 53
def find(options = {})
  Locator.locate self, options
end
hash() click to toggle source
# File lib/global_id/global_id.rb, line 86
def hash
  self.class.hash | @uri.hash
end
model_class() click to toggle source
# File lib/global_id/global_id.rb, line 57
  def model_class
    @model_class ||= begin
      locator = Locator.locator_for(self)
      model = begin
        locator.model_class(self)
      rescue NoMethodError
        if locator.respond_to?(:model_class)
          raise
        else
          GlobalID.deprecator.warn <<~MSG.squish
            Your locator #{locator.class.name} does not implement the
            `model_class` method. Please add a `model_class(gid)` method
            to your locator or inherit from `GlobalID::Locator::BaseLocator`.
          MSG
          model_name.constantize
        end
      end
      if model <= GlobalID
        raise ArgumentError, "GlobalID and SignedGlobalID cannot be used as model_class."
      end
      model
    end
  end
to_param() click to toggle source
# File lib/global_id/global_id.rb, line 90
def to_param
  Base64.urlsafe_encode64(to_s, padding: false)
end