class GlobalID - globalid-0.3.5 Documentation

# class GlobalID

### Attributes

app[R]
uri[R]

### Public Class Methods

app=(app) click to toggle source

    # File lib/global_id/global_id.rb, line 31
    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 }
        new URI::GID.create(app, model), 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 21
    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 50
    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 25
    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 36
    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 41
    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 62
    def ==(other)
      other.is_a?(GlobalID) && @uri == other.uri
    end

find(options = {}) click to toggle source

    # File lib/global_id/global_id.rb, line 54
    def find(options = {})
      Locator.locate self, options
    end

model_class() click to toggle source

    # File lib/global_id/global_id.rb, line 58
    def model_class
      model_name.constantize
    end

to_param() click to toggle source

    # File lib/global_id/global_id.rb, line 66
    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