Class: GlobalID

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Autoload
Defined in:
lib/global_id.rb,
lib/global_id/locator.rb,
lib/global_id/verifier.rb,
lib/global_id/global_id.rb,
lib/global_id/fixture_set.rb,
lib/global_id/identification.rb

Direct Known Subclasses

SignedGlobalID

Defined Under Namespace

Modules: FixtureSet, Identification, Locator Classes: Verifier

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gid, options = {}) ⇒ GlobalID

Returns a new instance of GlobalID.



44
45
46
# File 'lib/global_id/global_id.rb', line 44

def initialize(gid, options = {})
  @uri = gid.is_a?(URI::GID) ? gid : URI::GID.parse(gid)
end

Class Attribute Details

.appObject

Returns the value of attribute app.



9
10
11
# File 'lib/global_id/global_id.rb', line 9

def app
  @app
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



41
42
43
# File 'lib/global_id/global_id.rb', line 41

def uri
  @uri
end

Class Method Details

.create(model, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/global_id/global_id.rb', line 11

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

.deprecatorObject

:nodoc:



20
21
22
# File 'lib/global_id.rb', line 20

def self.deprecator # :nodoc:
  @deprecator ||= ActiveSupport::Deprecation.new("2.1", "GlobalID")
end

.eager_load!Object



15
16
17
18
# File 'lib/global_id.rb', line 15

def self.eager_load!
  super
  require 'global_id/signed_global_id'
end

.find(gid, options = {}) ⇒ Object



21
22
23
# File 'lib/global_id/global_id.rb', line 21

def find(gid, options = {})
  parse(gid, options).try(:find, options)
end

.parse(gid, options = {}) ⇒ Object



25
26
27
28
29
# 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

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



63
64
65
# File 'lib/global_id/global_id.rb', line 63

def ==(other)
  other.is_a?(GlobalID) && @uri == other.uri
end

#as_jsonObject



76
77
78
# File 'lib/global_id/global_id.rb', line 76

def as_json(*)
  to_s
end

#find(options = {}) ⇒ Object



48
49
50
# File 'lib/global_id/global_id.rb', line 48

def find(options = {})
  Locator.locate self, options
end

#hashObject



68
69
70
# File 'lib/global_id/global_id.rb', line 68

def hash
  self.class.hash | @uri.hash
end

#model_classObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/global_id/global_id.rb', line 52

def model_class
  @model_class ||= begin
    model = model_name.constantize

    if model <= GlobalID
      raise ArgumentError, "GlobalID and SignedGlobalID cannot be used as model_class."
    end
    model
  end
end

#to_paramObject



72
73
74
# File 'lib/global_id/global_id.rb', line 72

def to_param
  Base64.urlsafe_encode64(to_s, padding: false)
end