Class: DropletKit::BaseModel

Inherits:
Object
  • Object
show all
Defined in:
lib/droplet_kit/models/base_model.rb

Direct Known Subclasses

Account, AccountTeam, Action, App, AppAlertSpec, AppAutoscalingSpec, AppAutoscalingSpecMetricCPU, AppAutoscalingSpecMetrics, AppCorsPolicy, AppDatabaseSpec, AppDedicatedIp, AppDomain, AppDomainProgress, AppDomainSpec, AppDomainValidation, AppEgressSpec, AppFunctionSpec, AppGitHubSourceSpec, AppGitLabSourceSpec, AppGitSourceSpec, AppImageDeployOnPush, AppImageSourceSpec, AppIngressSpec, AppIngressSpecRule, AppIngressSpecRuleMatch, AppIngressSpecRuleRoutingComponent, AppIngressSpecRuleRoutingRedirect, AppIngressSpecRuleStringMatch, AppJobSpec, AppJobSpecTermination, AppLogDestinationSpec, AppLogDestinationSpecDatadog, AppLogDestinationSpecLogtail, AppLogDestinationSpecOpenSearch, AppLogDestinationSpecPapertrail, AppRegion, AppServiceSpec, AppServiceSpecHealthCheck, AppServiceSpecTermination, AppSpec, AppStaticSiteSpec, AppStringMatch, AppVariableDefinition, AppWorkerSpec, AppWorkerSpecTermination, Balance, CDN, Certificate, ContainerRegistry, ContainerRegistryRepository, ContainerRegistryRepositoryTag, Database, DatabaseBackup, DatabaseCluster, DatabaseConnection, DatabaseConnectionPool, DatabaseEvictionPolicy, DatabaseFirewallRule, DatabaseKafkaConfig, DatabaseMaintenanceWindow, DatabaseMetricsCredentials, DatabaseMongoConfig, DatabaseMysqlConfig, DatabaseOpensearchConfig, DatabasePostgresConfig, DatabasePostgresPgbouncerConfig, DatabasePostgresTimescaledbConfig, DatabaseRedisConfig, DatabaseReplica, DatabaseSQLMode, DatabaseUser, DatabaseUserMySQLSettings, DatabaseUserResetAuth, Deployment, DeploymentFunction, DeploymentJob, DeploymentProgress, DeploymentProgressStep, DeploymentProgressStepReason, DeploymentService, DeploymentStaticSite, DeploymentWorker, Domain, DomainRecord, Droplet, DropletUpgrade, Firewall, FirewallInboundRule, FirewallOutboundRule, FirewallPendingChange, FirewallRule, FloatingIp, ForwardingRule, HealthCheck, Image, Invoice, Kernel, KubernetesCluster, KubernetesMaintenancePolicy, KubernetesNode, KubernetesNodePool, KubernetesOptions, Links, LoadBalancer, MetaInformation, OneClick, OneClickKubernetes, OpenSearchBasicAuth, PaginationInformation, Project, ProjectAssignment, Region, ReservedIp, ReservedIpv6, SSHKey, Size, Snapshot, StickySession, Tag, TaggedDropletsResources, TaggedImagesResources, TaggedResources, VPC, VPCMember, VPCPeering, Volume

Constant Summary collapse

DO_NAMESPACE =
'do'
UNSUPPORTED_COLLECTIONS =
['space'].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_identifier(identifier) ⇒ Object



63
64
65
# File 'lib/droplet_kit/models/base_model.rb', line 63

def self.from_identifier(identifier)
  new(id: identifier)
end

.from_urn(urn) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/droplet_kit/models/base_model.rb', line 50

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) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/droplet_kit/models/base_model.rb', line 34

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

Instance Method Details

#collection_nameObject



23
24
25
# File 'lib/droplet_kit/models/base_model.rb', line 23

def collection_name
  DropletKit::Utils.underscore self.class.name.split('::').last
end

#identifierObject

Raises:



27
28
29
30
31
32
# File 'lib/droplet_kit/models/base_model.rb', line 27

def identifier
  identifier = attributes[:id] || attributes[:uuid] || attributes[:slug]
  raise DropletKit::Error, "#{self.class.name} doesn't support URNs" if identifier.nil?

  identifier
end

#inspectObject



14
15
16
17
# File 'lib/droplet_kit/models/base_model.rb', line 14

def inspect
  values = instance_variables.map { |name| [name, instance_variable_get(name)] }.to_h
  "<#{self.class.name} #{values}>"
end

#urnObject



19
20
21
# File 'lib/droplet_kit/models/base_model.rb', line 19

def urn
  "#{DO_NAMESPACE}:#{collection_name}:#{identifier}"
end