Class: GlobalID

Inherits:
Object
  • Object
show all
Defined in:
lib/global_id.rb,
lib/global_id/locator.rb,
lib/global_id/global_id.rb,
lib/global_id/identification.rb

Defined Under Namespace

Modules: Identification, Locator

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.



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

def initialize(gid, options = {})
  extract_uri_components 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

#appObject (readonly)

Returns the value of attribute app.



50
51
52
# File 'lib/global_id/global_id.rb', line 50

def app
  @app
end

#model_idObject (readonly)

Returns the value of attribute model_id.



50
51
52
# File 'lib/global_id/global_id.rb', line 50

def model_id
  @model_id
end

#model_nameObject (readonly)

Returns the value of attribute model_name.



50
51
52
# File 'lib/global_id/global_id.rb', line 50

def model_name
  @model_name
end

#uriObject (readonly)

Returns the value of attribute uri.



50
51
52
# File 'lib/global_id/global_id.rb', line 50

def uri
  @uri
end

Class Method Details

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

Raises:

  • (ArgumentError)


11
12
13
14
15
# File 'lib/global_id/global_id.rb', line 11

def create(model, options = {})
  app = options.fetch :app, GlobalID.app
  raise ArgumentError, "An app is required to create a GlobalID. Pass the :app option or set the default GlobalID.app." unless app
  new URI("gid://#{app}/#{model.class.name}/#{model.id}"), options
end

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



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

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

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



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

def parse(gid, options = {})
  gid.is_a?(self) ? gid : new(gid, options)
rescue URI::Error
  parse_encoded_gid(gid, options)
end

.validate_app(app) ⇒ Object



31
32
33
34
35
36
# File 'lib/global_id/global_id.rb', line 31

def validate_app(app)
  URI.parse('gid:///').hostname = app
rescue URI::InvalidComponentError
  raise ArgumentError, 'Invalid app name. ' \
    'App names must be valid URI hostnames: alphanumeric and hyphen characters only.'
end

Instance Method Details

#==(other) ⇒ Object



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

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

#find(options = {}) ⇒ Object



56
57
58
# File 'lib/global_id/global_id.rb', line 56

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

#model_classObject



60
61
62
# File 'lib/global_id/global_id.rb', line 60

def model_class
  model_name.constantize
end

#to_paramObject



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

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

#to_sObject



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

def to_s
  @uri.to_s
end