Class: Hyperclient::Collection
- Inherits:
-
Object
- Object
- Hyperclient::Collection
- Includes:
- Enumerable
- Defined in:
- lib/hyperclient/collection.rb
Overview
A helper class to wrap a collection of elements and provide Hash-like access or via a method call.
Direct Known Subclasses
Instance Method Summary collapse
-
#[](name) ⇒ Object
Provides Hash-like access to the collection.
-
#each(&block) ⇒ Object
Each implementation to allow the class to use the Enumerable benefits.
-
#fetch(*args) ⇒ Object
Returns a value from the collection for the given key.
-
#include?(key) ⇒ Boolean
Checks if this collection includes a given key.
-
#initialize(collection) ⇒ Collection
constructor
Initializes the Collection.
-
#method_missing(method_name, *_args, &_block) ⇒ Object
Provides method access to the collection values.
-
#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
private
Accessory method to allow the collection respond to the methods that will hit method_missing.
-
#to_h ⇒ Object
(also: #to_hash)
Returns the wrapped collection as a Hash.
- #to_s ⇒ Object
Constructor Details
#initialize(collection) ⇒ Collection
Initializes the Collection.
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`
77 78 79 80 81 |
# File 'lib/hyperclient/collection.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.
55 56 57 |
# File 'lib/hyperclient/collection.rb', line 55 def [](name) @collection[name.to_s] end |
#each(&block) ⇒ Object
Each implementation to allow the class to use the Enumerable benefits.
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;
46 47 48 |
# File 'lib/hyperclient/collection.rb', line 46 def fetch(*args) @collection.fetch(*args) end |
#include?(key) ⇒ Boolean
Checks if this collection includes a given key.
33 34 35 |
# File 'lib/hyperclient/collection.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.
85 86 87 |
# File 'lib/hyperclient/collection.rb', line 85 def respond_to_missing?(method_name, _include_private = false) @collection.include?(method_name.to_s) end |
#to_h ⇒ Object Also known as: to_hash
Returns the wrapped collection as a Hash.
62 63 64 |
# File 'lib/hyperclient/collection.rb', line 62 def to_h @collection.to_hash end |
#to_s ⇒ Object
67 68 69 |
# File 'lib/hyperclient/collection.rb', line 67 def to_s to_hash end |