Class: ActiveZone::Provider::ZoneCollection Abstract

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/active_zone/provider/zone_collection.rb

Overview

This class is abstract.

Subclass and override to create a queryable zone collection

A queryable array-like class representing a collection of Zone at a provider

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider, set = nil) ⇒ Object

Initialize new ZoneCollection

Parameters:

  • provider (ActiveZone::Provider)

    the provider

  • set (Array) (defaults to: nil)

    Array of names in the ZoneCollection, or nil for all zones



19
20
21
22
# File 'lib/active_zone/provider/zone_collection.rb', line 19

def initialize(provider, set = nil)
  @provider = provider
  @set = set
end

Instance Attribute Details

#providerProvider (readonly)

Provider the ZoneCollection is bound to

Returns:



9
10
11
# File 'lib/active_zone/provider/zone_collection.rb', line 9

def provider
  @provider
end

#setArray?

Set of names in the ZoneCollection

Returns:

  • (Array)
  • (nil)

    if all zones is desired



13
14
15
# File 'lib/active_zone/provider/zone_collection.rb', line 13

def set
  @set
end

Instance Method Details

#[](i) ⇒ Zone

This method is abstract.

Override and get the nth value in the set at the provider

Get nth value in collection

Returns:

Raises:

  • (NotImplementedError)


76
77
78
# File 'lib/active_zone/provider/zone_collection.rb', line 76

def [](i)
  raise NotImplementedError
end

#destroy_allnil

This method is abstract.

Override and destroy all zones in the set at the provider

Destroy all zones in this collection

Returns:

  • (nil)

Raises:

  • (NotImplementedError)


36
37
38
# File 'lib/active_zone/provider/zone_collection.rb', line 36

def destroy_all
  raise NotImplementedError
end

#each {|Zone| ... } ⇒ Object

This method is abstract.

Override and yield all zones in the set at the provider

Iterate over the zones at the provider

Yields:

  • (Zone)

    Zones within the #set at the provider

Raises:

  • (NotImplementedError)


28
29
30
# File 'lib/active_zone/provider/zone_collection.rb', line 28

def each(&block)
  raise NotImplementedError
end

#find(name) ⇒ Zone

This method is abstract.

Override and find the first zone by name in the set at the provider

Find a zone by name

Returns:

Raises:

  • (NotImplementedError)


44
45
46
# File 'lib/active_zone/provider/zone_collection.rb', line 44

def find(name)
  raise NotImplementedError
end

#find_by(**attributes) ⇒ Zone

This method is abstract.

Override and find the first zone that matches the provided attributes in the set at the provider

Find a zone by attribute in this collection

Parameters:

  • attributes (Hash)

    attributes to filter by

Returns:

Raises:

  • (NotImplementedError)


53
54
55
# File 'lib/active_zone/provider/zone_collection.rb', line 53

def find_by(**attributes)
  raise NotImplementedError
end

#find_or_create_by(**attributes) ⇒ Zone

Find a zone by name inside this collection, if not found then create it

Parameters:

  • attributes (Hash)

    the attributes of the zone to find by or create with

Returns:



60
61
62
# File 'lib/active_zone/provider/zone_collection.rb', line 60

def find_or_create_by(**attributes)
  find_by(attributes) || provider.Zone.create(attributes)
end

#upsert(name) ⇒ Zone

Update a zone by name in this collection, if not found then create it

Parameters:

  • name (String)

    the name of the zone

Returns:



67
68
69
70
# File 'lib/active_zone/provider/zone_collection.rb', line 67

def upsert(name)
  z = find_or_create_by(name:)
  z.update(attributes)
end