Class: Ubicloud::Context

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

Overview

Ubicloud::Context instances are the root object used in Ubicloud's Ruby SDK. They provide access to the models, using the configured adapter.

The following instance methods are defined via metaprogramming. All return instances of Ubicloud::ModelAdapter, for the related model.

firewall

Ubicloud::Firewall

load_balancer

Ubicloud::LoadBalancer

postgres

Ubicloud::Postgres

private_subnet

Ubicloud::PrivateSubnet

vm

Ubicloud::Vm

Constant Summary collapse

MODEL_PREFIX_MAP =
{
  "vm" => Vm,
  "pg" => Postgres,
  "fw" => Firewall,
  "ps" => PrivateSubnet,
  "1b" => LoadBalancer
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(adapter) ⇒ Context

Returns a new instance of Context.



16
17
18
19
# File 'lib/ubicloud/context.rb', line 16

def initialize(adapter)
  @adapter = adapter
  @models = {}
end

Instance Method Details

#[](id) ⇒ Object

The same as #new, but checks that the object exists and you have access to it.



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

def [](id)
  new(id)&.check_exists
end

#new(id) ⇒ Object

Return a new model instance for the given id, assuming the id is properly formatted. Returns nil if the id is not properly formatted. Does not check with Ubicloud to determine whether the object actually exists.



42
43
44
45
46
# File 'lib/ubicloud/context.rb', line 42

def new(id)
  if id.is_a?(String) && (model = MODEL_PREFIX_MAP[id[0, 2]]) && model.id_regexp.match?(id)
    model.new(@adapter, id)
  end
end