class DropletKit::BaseModel

Constants

DO_NAMESPACE
UNSUPPORTED_COLLECTIONS

Public Class Methods

from_identifier(identifier) click to toggle source
# File lib/droplet_kit/models/base_model.rb, line 61
def self.from_identifier(identifier)
  new(id: identifier)
end
from_urn(urn) click to toggle source
# File lib/droplet_kit/models/base_model.rb, line 48
def self.from_urn(urn)
  DropletKit::Error.new("Invalid urn: #{urn}") unless valid_urn?(urn)

  parts = urn.split(':')
  collection = parts[1]
  identifier = parts[2]

  return nil if UNSUPPORTED_COLLECTIONS.include?(collection)

  klass = const_get("DropletKit::#{DropletKit::Utils.camelize(collection)}")
  klass.from_identifier(identifier)
end
valid_urn?(urn) click to toggle source
# File lib/droplet_kit/models/base_model.rb, line 32
def self.valid_urn?(urn)
  parts = urn.split(':')
  return false if parts.size != 3 || parts[0] != DO_NAMESPACE

  collection = parts[1]
  return true if UNSUPPORTED_COLLECTIONS.include?(collection)

  begin
    const_get "DropletKit::#{DropletKit::Utils.camelize(collection)}"
  rescue NameError
    return false
  end

  true
end

Public Instance Methods

collection_name() click to toggle source
# File lib/droplet_kit/models/base_model.rb, line 21
def collection_name
  DropletKit::Utils.underscore self.class.name.split('::').last
end
identifier() click to toggle source
# File lib/droplet_kit/models/base_model.rb, line 25
def identifier
  identifier = attributes[:id] || attributes[:uuid] || attributes[:slug]
  raise DropletKit::Error.new("#{self.class.name} doesn't support URNs") if identifier.nil?

  identifier
end
inspect() click to toggle source
# File lib/droplet_kit/models/base_model.rb, line 12
def inspect
  values = Hash[instance_variables.map { |name| [name, instance_variable_get(name)] } ]
  "<#{self.class.name} #{values}>"
end
urn() click to toggle source
# File lib/droplet_kit/models/base_model.rb, line 17
def urn
  "#{DO_NAMESPACE}:#{collection_name}:#{identifier}"
end