class SkullIsland::ResourceCollection

The ResourceCollection class Should not allow or use mixed types

Attributes

type[R]

@return [Class] a collection of this {Resource} subclass

Public Class Methods

new(list, options = {}) click to toggle source
# File lib/skull_island/resource_collection.rb, line 13
def initialize(list, options = {})
  # TODO: better options validations
  raise Exceptions::InvalidOptions unless options.is_a?(Hash)
  raise Exceptions::InvalidArguments if list.empty? && options[:type].nil?

  @api_client = options[:api_client] || APIClient.instance
  @list = list
  @type = options[:type] || list.first.class
end

Public Instance Methods

+(other) click to toggle source

Return a collection after adding to the original

Warning: this may cause duplicates or mixed type joins! For safety,
use #merge

@return [ResourceCollection]

# File lib/skull_island/resource_collection.rb, line 163
def +(other)
  case other
  when self.class
    self.class.new(@list + other.to_a, type: @type, api_client: @api_client)
  when @type
    self.class.new(@list + [other], type: @type, api_client: @api_client)
  else
    raise Exceptions::InvalidArguments
  end
end
-(other) click to toggle source

Return a collection after subtracting from the original @return [ResourceCollection]

# File lib/skull_island/resource_collection.rb, line 149
def -(other)
  if other.respond_to?(:to_a)
    self.class.new(@list - other.to_a, type: @type, api_client: @api_client)
  elsif other.is_a?(Resource)
    self.class.new(@list - Array(other), type: @type, api_client: @api_client)
  else
    raise Exceptions::InvalidArguments
  end
end
<<(other) click to toggle source
# File lib/skull_island/resource_collection.rb, line 174
def <<(other)
  raise Exceptions::InvalidArguments, 'Resource Type Mismatch' unless other.instance_of?(@type)

  @list << other
end
<=>(other) click to toggle source
# File lib/skull_island/resource_collection.rb, line 180
def <=>(other)
  collect(&:id).sort <=> other.collect(&:id).sort
end
==(other) click to toggle source

Allow comparison of collection @return [Boolean] do the collections contain the same resource ids?

# File lib/skull_island/resource_collection.rb, line 186
def ==(other)
  if other.is_a? self.class
    collect(&:id).sort == other.collect(&:id).sort
  else
    false
  end
end
[](index) click to toggle source

Return the collection item at the specified index @return [Resource,ResourceCollection] the item at the requested index

# File lib/skull_island/resource_collection.rb, line 139
def [](index)
  if index.is_a?(Range)
    self.class.new(@list[index], type: @type, api_client: @api_client)
  else
    @list[index]
  end
end
and(attribute, value, options = {})
Alias for: where
each(&block) click to toggle source
# File lib/skull_island/resource_collection.rb, line 23
def each(&block)
  @list.each(&block)
end
empty?() click to toggle source

Does the collection contain anything? @return [Boolean]

# File lib/skull_island/resource_collection.rb, line 29
def empty?
  @list.empty?
end
first(number = nil) click to toggle source

Provide the first (or first `number`) entries @param number [Fixnum] How many to provide @return [ResourceCollection,Resource]

# File lib/skull_island/resource_collection.rb, line 36
def first(number = nil)
  if number
    self.class.new(@list.first(number), type: @type, api_client: @api_client)
  else
    @list.first
  end
end
last(number = nil) click to toggle source

Provide the last (or last `number`) entries @param number [Fixnum] How many to provide @return [ResourceCollection,Resource]

# File lib/skull_island/resource_collection.rb, line 47
def last(number = nil)
  if number
    self.class.new(@list.last(number), type: @type, api_client: @api_client)
  else
    @list.last
  end
end
merge(other) click to toggle source

Merge two collections @param other [ResourceCollection] @return [ResourceCollection]

# File lib/skull_island/resource_collection.rb, line 58
def merge(other)
  raise Exceptions::InvalidArguments unless other.is_a?(self.class)

  self + (other - self)
end
model() click to toggle source

An alias for {#type}

# File lib/skull_island/resource_collection.rb, line 65
def model
  type
end
or(attribute, value, options = {}) click to toggle source

Hacked together or() method in the same spirit as where(). This method can be chained for multiple / more specific queries.

@param attribute [Symbol] the attribute to query @param value [Object] the value to compare against

- allowed options are "'==', '!=', '>', '>=', '<', '<=', and 'match'"

@raise [Exceptions::InvalidWhereQuery] if not the right kind of comparison @return [ResourceCollection]

# File lib/skull_island/resource_collection.rb, line 77
def or(attribute, value, options = {})
  options[:comparison] ||= value.is_a?(Regexp) ? :match : '=='
  if empty?
    @type.where(attribute, value, comparison: options[:comparison], api_client: @api_client)
  else
    merge first.class.where(
      attribute, value,
      comparison: options[:comparison],
      api_client: @api_client
    )
  end
end
paginate(*args) click to toggle source

Pass pagination through to the Array (which passes to will_paginate)

# File lib/skull_island/resource_collection.rb, line 91
def paginate(*args)
  @list.paginate(*args)
end
size() click to toggle source

Returns the number of Resource instances in the collection @return [Fixnum]

# File lib/skull_island/resource_collection.rb, line 97
def size
  @list.size
end
sort(&block) click to toggle source

Allow complex sorting like an Array @return [ResourceCollection] sorted collection

Calls superclass method
# File lib/skull_island/resource_collection.rb, line 103
def sort(&block)
  self.class.new(super(&block), type: @type, api_client: @api_client)
end
where(attribute, value, options = {}) click to toggle source

Horribly inefficient way to allow querying Resources by their attributes. This method can be chained for multiple / more specific queries.

@param attribute [Symbol] the attribute to query @param value [Object] the value to compare against

- allowed options are "'==', '!=', '>', '>=', '<', '<=', and 'match'"

@raise [Exceptions::InvalidWhereQuery] if not the right kind of comparison @return [ResourceCollection]

# File lib/skull_island/resource_collection.rb, line 115
def where(attribute, value, options = {})
  valid_comparisons = %i[== != > >= < <= match]
  options[:comparison] ||= value.is_a?(Regexp) ? :match : '=='
  unless valid_comparisons.include?(options[:comparison].to_sym)
    raise Exceptions::InvalidWhereQuery
  end

  self.class.new(
    @list.collect do |item|
      if item.send(attribute).nil?
        nil
      elsif item.send(attribute).send(options[:comparison].to_sym, value)
        item
      end
    end.compact,
    type: @type,
    api_client: @api_client
  )
end
Also aliased as: and