Class: Hyperclient::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/hyperclient/collection.rb

Overview

A helper class to wrapp a collection of elements and provide Hash-like access or via a method call.

Examples:

collection['value']
collection.value

Direct Known Subclasses

Attributes, LinkCollection, ResourceCollection

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ Collection

Initializes the Collection.

Parameters:

  • collection

    The Hash to be wrapped.



16
17
18
# File 'lib/hyperclient/collection.rb', line 16

def initialize(collection)
  @collection = collection
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *_args, &_block) ⇒ Object

Provides method access to the collection values.

It allows accessing a value as ‘collection.name` instead of `collection`

Returns:

  • an Object.



72
73
74
75
76
# File 'lib/hyperclient/collection.rb', line 72

def method_missing(method_name, *_args, &_block)
  @collection.fetch(method_name.to_s)  do
    fail "Could not find `#{method_name}` in #{self.class.name}"
  end
end

Instance Method Details

#[](name) ⇒ Object

Provides Hash-like access to the collection.

Parameters:

  • name

    A String or Symbol of the value to get from the collection.

Returns:

  • an Object.



50
51
52
# File 'lib/hyperclient/collection.rb', line 50

def [](name)
  @collection[name.to_s]
end

#each(&block) ⇒ Object

Each implementation to allow the class to use the Enumerable benefits.

Returns:

  • an Enumerator.



24
25
26
# File 'lib/hyperclient/collection.rb', line 24

def each(&block)
  @collection.each(&block)
end

#fetch(*args) ⇒ Object

Returns a value from the collection for the given key. If the key can’t be found, there are several options: With no other arguments, it will raise an KeyError exception; if default is given, then that will be returned;

Parameters:

  • key

    A String or Symbol of the value to get from the collection.

  • default

    An optional value to be returned if the key is not found.

Returns:

  • an Object.



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

def fetch(*args)
  @collection.fetch(*args)
end

#include?(obj) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/hyperclient/collection.rb', line 28

def include?(obj)
  @collection.include?(obj)
end

#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Accessory method to allow the collection respond to the methods that will hit method_missing.

Returns:

  • (Boolean)


80
81
82
# File 'lib/hyperclient/collection.rb', line 80

def respond_to_missing?(method_name, _include_private = false)
  @collection.include?(method_name.to_s)
end

#to_hObject Also known as: to_hash

Returns the wrapped collection as a hash.

Returns:

  • a Hash.



57
58
59
# File 'lib/hyperclient/collection.rb', line 57

def to_h
  @collection.to_hash
end

#to_sObject



62
63
64
# File 'lib/hyperclient/collection.rb', line 62

def to_s
  to_hash
end