class JSONAPI::Collection

Models a collection of items

Public Class Methods

new(arr_of_obj = [], item_type: Object, &block) click to toggle source

Assume collection is empty not innitialized with an array of objects. @param arr_of_obj [Object] The objects to be stored for block { |item| item } @yield [item] Determines what should be used as keys when storing objects in collection's internal hash

# 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

Public Instance Methods

[](key) click to toggle source

Alias to get @param (see get) @param (see get)

# File lib/easy/jsonapi/collection.rb, line 90
def [](key)
  get(key)
end
[]=(key, item) click to toggle source

Alias to set @param (see set) @param (see set)

# File lib/easy/jsonapi/collection.rb, line 97
def []=(key, item)
  set(key, item)
end
add(item, &block) click to toggle source

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

collection should create a hash key for the item.

@param item [Object]

# 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
each() { |item| ... } click to toggle source

Yield the block given on all the items in the collection

# File lib/easy/jsonapi/collection.rb, line 68
def each
  return @collection.each { |_, item| yield(item) } if block_given?
  to_enum(:each)
end
empty?() click to toggle source

Checks to see if the collection is empty @return [TrueClass | FalseClass]

# File lib/easy/jsonapi/collection.rb, line 33
def empty?
  @collection == {}
end
get(key) click to toggle source

@param (see remove) @return [Item | nil] The appropriate Item object if it exists

# File lib/easy/jsonapi/collection.rb, line 83
def get(key)
  @collection[key.to_sym]
end
include?(key) click to toggle source

Does the collection's internal hash include this key? @param key [String | Symbol] The key to search for in the hash

# File lib/easy/jsonapi/collection.rb, line 39
def include?(key)
  @collection.include?(key.to_sym)
end
keys() click to toggle source

Allows the developer to treat the Collection class as a hash, retrieving all keys mapped to Items. @return [Array<Symbol>] An array of all the item keys stored in the Collection object.

# File lib/easy/jsonapi/collection.rb, line 103
def keys
  @collection.keys
end
remove(key) click to toggle source

Remove an item from the collection @param (see include) @return [Item | nil] the deleted item object if it exists

# 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) click to toggle source

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

# 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
size() click to toggle source

@return [Integer] The number of items in the collection

# File lib/easy/jsonapi/collection.rb, line 108
def size
  @collection.size
end
to_s() click to toggle source

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

# 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

Protected Instance Methods

insert(key, item) click to toggle source

Adds an item to Collection's internal hash

# 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

Private Instance Methods

method_missing(method_name, *args, &block) click to toggle source

Gets the Collection object whose hash key matches the method_name called @param method_name [Symbol] The name of the method called @param args If any arguments were passed to the method called @param block If a block was passed to the method called

Calls superclass method
# 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
respond_to_missing?(method_name, *) click to toggle source

Whether or not method missing should be called.

Calls superclass method
# File lib/easy/jsonapi/collection.rb, line 140
def respond_to_missing?(method_name, *)
  @collection.include?(method_name) || super
end