Class: PlateID

Inherits:
Object
  • Object
show all
Defined in:
lib/plate_id.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plate_id) ⇒ PlateID

Returns a new instance of PlateID.



45
46
47
# File 'lib/plate_id.rb', line 45

def initialize(plate_id)
  @uri = fetch_uri(plate_id)
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



23
24
25
# File 'lib/plate_id.rb', line 23

def uri
  @uri
end

Class Method Details

.create(model) ⇒ Object



6
7
8
9
# File 'lib/plate_id.rb', line 6

def create(model)
  uri = URI::PlateID.create(model)
  new(uri) if uri
end

.find(plate_id) ⇒ Object



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

def find(plate_id)
  res = parse(plate_id)
  res&.find
end

.parse(plate_id) ⇒ Object



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

def parse(plate_id)
  plate_id.is_a?(self) ? plate_id : new(plate_id)
rescue URI::Error
  nil
end

Instance Method Details

#==(other) ⇒ Object



59
60
61
# File 'lib/plate_id.rb', line 59

def ==(other)
  to_s == other.to_s
end

#base_classObject



29
30
31
# File 'lib/plate_id.rb', line 29

def base_class
  uri.base_class
end

#findObject



53
54
55
56
57
# File 'lib/plate_id.rb', line 53

def find
  return unless uri.id

  plate_class&.find_by(id: uri.id)
end

#hostObject



25
26
27
# File 'lib/plate_id.rb', line 25

def host
  uri.host
end

#idObject



33
34
35
# File 'lib/plate_id.rb', line 33

def id
  uri.id
end

#id=(id) ⇒ Object



37
38
39
# File 'lib/plate_id.rb', line 37

def id=(id)
  uri.id = id
end

#plate_classObject



49
50
51
# File 'lib/plate_id.rb', line 49

def plate_class
  fetch_plate_class(uri)&.safe_constantize
end

#to_sObject



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

def to_s
  uri.to_s
end