Class: JSONAPI::NameValuePairCollection
- Inherits:
-
Collection
- Object
- Collection
- JSONAPI::NameValuePairCollection
- Defined in:
- lib/easy/jsonapi/name_value_pair_collection.rb
Overview
Collection of Items that all have names and values.
Direct Known Subclasses
Document::Error, Document::Jsonapi, Document::Links, Document::Meta, Document::Resource::Attributes, Document::Resource::Relationships, HeaderCollection, Request::QueryParamCollection
Instance Method Summary collapse
-
#<<(pair, &block) ⇒ Object
Another way to add a query_param.
-
#add(pair, &block) ⇒ Object
Add a pair to the collection.
-
#initialize(pair_arr = [], item_type: JSONAPI::NameValuePair, &block) ⇒ NameValuePairCollection
constructor
Creates an empty collection by default.
-
#to_h ⇒ Hash
Represent the collection as a hash.
-
#to_s ⇒ String
Represent the collection as a string.
Methods inherited from Collection
#[], #[]=, #each, #empty?, #get, #include?, #insert, #keys, #remove, #set, #size
Constructor Details
#initialize(pair_arr = [], item_type: JSONAPI::NameValuePair, &block) ⇒ NameValuePairCollection
Creates an empty collection by default
14 15 16 17 18 19 20 |
# File 'lib/easy/jsonapi/name_value_pair_collection.rb', line 14 def initialize(pair_arr = [], item_type: JSONAPI::NameValuePair, &block) if block_given? super(pair_arr, item_type: item_type, &block) else super(pair_arr, item_type: item_type, &:name) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object (private)
Gets the NameValuePair object value whose name matches the method_name called
67 68 69 70 |
# File 'lib/easy/jsonapi/name_value_pair_collection.rb', line 67 def method_missing(method_name, *args, &block) super unless include?(method_name) get(method_name).value end |
Instance Method Details
#<<(pair, &block) ⇒ Object
Another way to add a query_param
37 38 39 |
# File 'lib/easy/jsonapi/name_value_pair_collection.rb', line 37 def <<(pair, &block) add(pair, &block) end |
#add(pair, &block) ⇒ Object
Add a pair to the collection. (CASE-SENSITIVE)
27 28 29 30 31 32 33 |
# File 'lib/easy/jsonapi/name_value_pair_collection.rb', line 27 def add(pair, &block) if block_given? super(pair, &block) else super(pair, &:name) end end |
#to_h ⇒ Hash
Represent the collection as a hash
55 56 57 |
# File 'lib/easy/jsonapi/name_value_pair_collection.rb', line 55 def to_h JSONAPI::Utility.to_h_collection(self) end |
#to_s ⇒ String
Represent the collection as a string
49 50 51 |
# File 'lib/easy/jsonapi/name_value_pair_collection.rb', line 49 def to_s JSONAPI::Utility.to_string_collection(self, pre_string: '{ ', post_string: ' }') end |