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/railtie.rb,
lib/global_id/version.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: Railtie, Verifier

Constant Summary collapse

VERSION =
"1.4.0"

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.



49
50
51
# 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

Class Attribute Details

.appObject

Returns the value of attribute app.



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

def app
  @app
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



46
47
48
# File 'lib/global_id/global_id.rb', line 46

def uri
  @uri
end

Class Method Details

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



12
13
14
15
16
17
18
19
20
# 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) ⇒ Object



36
37
38
# File 'lib/global_id/global_id.rb', line 36

def default_locator(default_locator)
  Locator.default_locator = default_locator
end

.deprecatorObject

:nodoc:



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

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

.eager_load!Object



17
18
19
20
# File 'lib/global_id.rb', line 17

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

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



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

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

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



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

Instance Method Details

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



81
82
83
# File 'lib/global_id/global_id.rb', line 81

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

#as_jsonObject



94
95
96
# File 'lib/global_id/global_id.rb', line 94

def as_json(*)
  to_s
end

#find(options = {}) ⇒ Object



53
54
55
# File 'lib/global_id/global_id.rb', line 53

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

#hashObject



86
87
88
# File 'lib/global_id/global_id.rb', line 86

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

#model_classObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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_paramObject



90
91
92
# File 'lib/global_id/global_id.rb', line 90

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