class GlobalID

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
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 51
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 37
def parse_encoded_gid(gid, options)
  new(Base64.urlsafe_decode64(repad_gid(gid)), options) rescue nil
end
repad_gid(gid) click to toggle source

We removed the base64 padding character = during to_param, now we're adding it back so decoding will work

# File lib/global_id/global_id.rb, line 42
def repad_gid(gid)
  padding_chars = gid.length.modulo(4).zero? ? 0 : (4 - gid.length.modulo(4))
  gid + ('=' * padding_chars)
end

Public Instance Methods

==(other) click to toggle source
# File lib/global_id/global_id.rb, line 63
def ==(other)
  other.is_a?(GlobalID) && @uri == other.uri
end
find(options = {}) click to toggle source
# File lib/global_id/global_id.rb, line 55
def find(options = {})
  Locator.locate self, options
end
model_class() click to toggle source
# File lib/global_id/global_id.rb, line 59
def model_class
  model_name.constantize
end
to_param() click to toggle source
# File lib/global_id/global_id.rb, line 67
def to_param
  # remove the = padding character for a prettier param -- it'll be added back in parse_encoded_gid
  Base64.urlsafe_encode64(to_s).sub(/=+$/, '')
end