Class: ApiNavigator::CollectionHash

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/api_navigator/collection_hash.rb

Overview

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

Examples:

collection['value']
collection.value

Direct Known Subclasses

Attributes, LinkCollection

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ CollectionHash

Initializes the Collection.

Parameters:

  • collection

    The Hash to be wrapped.



16
17
18
# File 'lib/api_navigator/collection_hash.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.



77
78
79
80
81
# File 'lib/api_navigator/collection_hash.rb', line 77

def method_missing(method_name, *_args, &_block)
  @collection.fetch(method_name.to_s) do
    raise "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.



55
56
57
# File 'lib/api_navigator/collection_hash.rb', line 55

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/api_navigator/collection_hash.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.



46
47
48
# File 'lib/api_navigator/collection_hash.rb', line 46

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

#include?(key) ⇒ Boolean

Checks if this collection includes a given key.

Parameters:

  • key

    A String or Symbol to check for existance.

Returns:

  • (Boolean)

    True/False.



33
34
35
# File 'lib/api_navigator/collection_hash.rb', line 33

def include?(key)
  @collection.include?(key)
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)


85
86
87
# File 'lib/api_navigator/collection_hash.rb', line 85

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.



62
63
64
# File 'lib/api_navigator/collection_hash.rb', line 62

def to_h
  @collection.to_hash
end

#to_sObject



67
68
69
# File 'lib/api_navigator/collection_hash.rb', line 67

def to_s
  to_hash
end