Class: JSONAPI::Collection

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

Overview

Models a collection of items

Direct Known Subclasses

NameValuePairCollection

Instance Method Summary collapse

Constructor Details

#initialize(arr_of_obj = [], item_type: Object) {|item| ... } ⇒ Collection

Assume collection is empty not innitialized with an array of objects. for block { |item| item }

Parameters:

  • arr_of_obj (Object) (defaults to: [])

    The objects to be stored

Yields:

  • (item)

    Determines what should be used as keys when storing objects in collection’s internal hash



12
13
14
15
16
17
18
19
20
21
# File 'lib/easy/jsonapi/collection.rb', line 12

def initialize(arr_of_obj = [], item_type: Object, &block)
  @item_type = item_type
  @collection = {}

  return unless (arr_of_obj != []) && block_given?

  arr_of_obj.each do |obj|
    add(obj, &block)
  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 Collection object whose hash key 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



134
135
136
137
# File 'lib/easy/jsonapi/collection.rb', line 134

def method_missing(method_name, *args, &block)
  super unless @collection.include?(method_name)
  get(method_name)
end

Instance Method Details

#[](key) ⇒ Object

Alias to #get



90
91
92
# File 'lib/easy/jsonapi/collection.rb', line 90

def [](key)
  get(key)
end

#[]=(key, item) ⇒ Object

Alias to #set



97
98
99
# File 'lib/easy/jsonapi/collection.rb', line 97

def []=(key, item)
  set(key, item)
end

#add(item, &block) ⇒ Object

Add an item to the collection, giving a block to indicate how the

collection should create a hash key for the item.

Parameters:

  • item (Object)


46
47
48
49
50
# File 'lib/easy/jsonapi/collection.rb', line 46

def add(item, &block)
  raise 'a block must be passed to #add indicating what should be used as a key' unless block_given?
  raise "Cannot add an item that is not #{@item_type}" unless item.is_a? @item_type
  insert(block.call(item), item)
end

#eachObject

Yield the block given on all the items in the collection



68
69
70
71
# File 'lib/easy/jsonapi/collection.rb', line 68

def each
  return @collection.each { |_, item| yield(item) } if block_given?
  to_enum(:each)
end

#empty?TrueClass | FalseClass

Checks to see if the collection is empty

Returns:

  • (TrueClass | FalseClass)


33
34
35
# File 'lib/easy/jsonapi/collection.rb', line 33

def empty?
  @collection == {}
end

#get(key) ⇒ Item | nil

Returns The appropriate Item object if it exists.

Returns:

  • (Item | nil)

    The appropriate Item object if it exists



83
84
85
# File 'lib/easy/jsonapi/collection.rb', line 83

def get(key)
  @collection[key.to_sym]
end

#include?(key) ⇒ Boolean

Does the collection’s internal hash include this key?

Parameters:

  • key (String | Symbol)

    The key to search for in the hash

Returns:

  • (Boolean)


39
40
41
# File 'lib/easy/jsonapi/collection.rb', line 39

def include?(key)
  @collection.include?(key.to_sym)
end

#insert(key, item) ⇒ Object

Adds an item to Collection’s internal hash



53
54
55
56
57
58
59
# File 'lib/easy/jsonapi/collection.rb', line 53

def insert(key, item)
  if include?(key)
    raise 'The hash key given already has an Item associated with it. ' \
          'Remove existing item first.'
  end
  set(key, item)
end

#keysArray<Symbol>

Allows the developer to treat the Collection class as a hash, retrieving all keys mapped to Items.

Returns:

  • (Array<Symbol>)

    An array of all the item keys stored in the Collection object.



103
104
105
# File 'lib/easy/jsonapi/collection.rb', line 103

def keys
  @collection.keys
end

#remove(key) ⇒ Item | nil

Remove an item from the collection

Returns:

  • (Item | nil)

    the deleted item object if it exists



76
77
78
79
# File 'lib/easy/jsonapi/collection.rb', line 76

def remove(key)
  return nil if @collection[key.to_sym].nil?
  @collection.delete(key.to_sym)
end

#set(key, item) ⇒ Object

Overwrites the item associated w a given key, or adds an association if no item is already associated.



62
63
64
65
# File 'lib/easy/jsonapi/collection.rb', line 62

def set(key, item)
  raise "Cannot add an item that is not #{@item_type}" unless item.is_a? @item_type
  @collection[key.to_sym] = item
end

#sizeInteger

Returns The number of items in the collection.

Returns:

  • (Integer)

    The number of items in the collection



108
109
110
# File 'lib/easy/jsonapi/collection.rb', line 108

def size
  @collection.size
end

#to_sObject

Used to print out the Collection object with better formatting return [String] The collection object contents represented as a formatted string



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/easy/jsonapi/collection.rb', line 114

def to_s
  to_return = '{ '
  is_first = true
  @collection.each do |k, item|
    if is_first
      to_return += "\"#{k}\": #{item}"
      is_first = false
    else
      to_return += ", \"#{k}\": #{item}"
    end
  end
  to_return += ' }'
end