Class: JSONAPI::NameValuePairCollection

Inherits:
Collection
  • Object
show all
Defined in:
lib/easy/jsonapi/name_value_pair_collection.rb

Overview

Collection of Items that all have names and values.

Instance Method Summary collapse

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

Parameters:



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

Parameters:

  • method_name (Symbol)

    The name of the method called

  • args

    If any arguments were passed to the method called

  • block

    If a block was passed to the method 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)

Parameters:



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_hHash

Represent the collection as a hash

Returns:

  • (Hash)

    The representation of the collection



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_sString

Represent the collection as a string

Returns:

  • (String)

    The representation of the collection



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