Class: Antaeus::ResourceCollection

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
lib/antaeus-sdk/resource_collection.rb

Overview

The ResourceCollection class Should not allow or use mixed types

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list, options = {}) ⇒ ResourceCollection

Returns a new instance of ResourceCollection.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/antaeus-sdk/resource_collection.rb', line 11

def initialize(list, options = {})
  raise Exceptions::InvalidOptions unless options.is_a?(Hash)
  raise Exceptions::MissingAPIClient unless options[:client]
  raise Exceptions::InvalidAPIClient unless options[:client].is_a?(APIClient)
  raise Exceptions::InvalidInput if list.empty? and options[:type].nil?
  @client = options[:client]
  @list = list
  if options[:type]
    @type = options[:type]
  else
    @type = list.first.class
  end
end

Instance Attribute Details

#typeClass (readonly)

Returns this is a collection of this Antaeus::Resource subclass.

Returns:



9
10
11
# File 'lib/antaeus-sdk/resource_collection.rb', line 9

def type
  @type
end

Instance Method Details

#+(other) ⇒ ResourceCollection

Return a collection after adding to the original

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

Returns:



166
167
168
169
170
171
172
173
174
# File 'lib/antaeus-sdk/resource_collection.rb', line 166

def +(other)
  if other.is_a?(self.class)
    self.class.new(@list + other.to_a, type: @type, client: @client)
  elsif other.is_a?(@type)
    self.class.new(@list + [other], type: @type, client: @client)
  else
    fail Exceptions::InvalidInput
  end
end

#-(other) ⇒ ResourceCollection

Return a collection after subtracting from the original

Returns:



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/antaeus-sdk/resource_collection.rb', line 148

def -(other)
  new_list = @list.dup
  if other.respond_to?(:to_a)
    other.to_a.each do |item|
      new_list.delete_if { |res| res.id == item.id }
    end
  elsif other.is_a?(Resource)
    new_list.delete_if { |res| res.id == other.id }
  else
    raise Exceptions::InvalidInput
  end
  self.class.new(new_list, type: @type, client: @client)
end

#<<(other) ⇒ Object



176
177
178
179
180
181
182
# File 'lib/antaeus-sdk/resource_collection.rb', line 176

def <<(other)
  if other.class == @type
    @list << other
  else
    fail Exceptions::InvalidInput
  end
end

#<=>(other) ⇒ Object



184
185
186
# File 'lib/antaeus-sdk/resource_collection.rb', line 184

def <=>(other)
  collect(&:id).sort <=> other.collect(&:id).sort
end

#==(other) ⇒ Boolean

Allow comparison of collection

Returns:

  • (Boolean)

    do the collections contain the same resource ids?



190
191
192
193
194
195
196
# File 'lib/antaeus-sdk/resource_collection.rb', line 190

def ==(other)
  if other.is_a? self.class
    collect(&:id).sort == other.collect(&:id).sort
  else
    false
  end
end

#[](index) ⇒ Resource, ResourceCollection

Return the collection item at the specified index

Returns:



138
139
140
141
142
143
144
# File 'lib/antaeus-sdk/resource_collection.rb', line 138

def [](index)
  if index.is_a?(Range)
    self.class.new(@list[index], type: @type, client: @client)
  else
    @list[index]
  end
end

#each(&block) ⇒ Object



25
26
27
# File 'lib/antaeus-sdk/resource_collection.rb', line 25

def each(&block)
  @list.each(&block)
end

#empty?Boolean

Does the collection contain anything?

Returns:

  • (Boolean)


31
32
33
# File 'lib/antaeus-sdk/resource_collection.rb', line 31

def empty?
  @list.empty?
end

#first(n = nil) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/antaeus-sdk/resource_collection.rb', line 35

def first(n = nil)
  if n
    self.class.new(@list.first(n), type: @type, client: @client)
  else
    @list.first
  end
end

#last(n = nil) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/antaeus-sdk/resource_collection.rb', line 43

def last(n = nil)
  if n
    self.class.new(@list.last(n), type: @type, client: @client)
  else
    @list.last
  end
end

#merge(other) ⇒ ResourceCollection

Merge two collections

Returns:



53
54
55
56
57
58
59
60
# File 'lib/antaeus-sdk/resource_collection.rb', line 53

def merge(other)
  if other.is_a?(self.class)
    new_list = @list.dup
    self + (other - self)
  else
    fail Exceptions::InvalidInput
  end
end

#modelObject

Makes #model compatible with the server-side



63
64
65
# File 'lib/antaeus-sdk/resource_collection.rb', line 63

def model
  type
end

#or(attribute, value, options = {}) ⇒ ResourceCollection

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

Parameters:

  • attribute (Symbol)

    the attribute to query

  • value (Object)

    the value to compare against

  • comparison_method (String, Symbol)

    the method to use for comparison

    • allowed options are “‘==’, ‘!=’, ‘>’, ‘>=’, ‘<’, ‘<=’, and ‘match’”

Returns:

Raises:



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/antaeus-sdk/resource_collection.rb', line 76

def or(attribute, value, options = {})
  options[:comparison] ||= '=='
  if empty?
    @type.where(attribute, value, comparison: options[:comparison], client: @client)
  else
    merge first.class.where(
      attribute, value,
      comparison: options[:comparison],
      client: @client
    )
  end
end

#paginate(*args) ⇒ Object

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



90
91
92
# File 'lib/antaeus-sdk/resource_collection.rb', line 90

def paginate(*args)
  @list.paginate(*args)
end

#sizeFixnum

Returns the number of Resource instances in the collection

Returns:

  • (Fixnum)


96
97
98
# File 'lib/antaeus-sdk/resource_collection.rb', line 96

def size
  @list.size
end

#sort(&block) ⇒ ResourceCollection

Allow complex sorting like an Array

Returns:



102
103
104
# File 'lib/antaeus-sdk/resource_collection.rb', line 102

def sort(&block)
  self.class.new(super(&block), type: @type, client: @client)
end

#where(attribute, value, options = {}) ⇒ ResourceCollection Also known as: and

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

Parameters:

  • attribute (Symbol)

    the attribute to query

  • value (Object)

    the value to compare against

  • comparison_method (String, Symbol)

    the method to use for comparison

    • allowed options are “‘==’, ‘!=’, ‘>’, ‘>=’, ‘<’, ‘<=’, and ‘match’”

Returns:

Raises:



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/antaeus-sdk/resource_collection.rb', line 115

def where(attribute, value, options = {})
  valid_comparisons = [:'==', :'!=', :>, :'>=', :<, :'<=', :match]
  options[:comparison] ||= '=='
  unless valid_comparisons.include?(options[:comparison].to_sym)
    fail Exceptions::InvalidWhereQuery
  end
  self.class.new(
    @list.collect do |item|
      if item.send(attribute).nil?
        nil
      else
        item if item.send(attribute).send(options[:comparison].to_sym, value)
      end
    end.compact,
    type: @type,
    client: @client
  )
end