Class: KnuVerse::Knufactor::ResourceCollection

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
lib/knuverse/knufactor/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.



12
13
14
15
16
17
18
19
# File 'lib/knuverse/knufactor/resource_collection.rb', line 12

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

Instance Attribute Details

#typeClass (readonly)

Returns a collection of this KnuVerse::Knufactor::Resource subclass.

Returns:



10
11
12
# File 'lib/knuverse/knufactor/resource_collection.rb', line 10

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:



159
160
161
162
163
164
165
166
167
# File 'lib/knuverse/knufactor/resource_collection.rb', line 159

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

#-(other) ⇒ ResourceCollection

Return a collection after subtracting from the original

Returns:



145
146
147
148
149
150
151
152
153
# File 'lib/knuverse/knufactor/resource_collection.rb', line 145

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) ⇒ Object



169
170
171
172
# File 'lib/knuverse/knufactor/resource_collection.rb', line 169

def <<(other)
  raise Exceptions::InvalidArguments, 'Resource Type Mismatch' unless other.class == @type
  @list << other
end

#<=>(other) ⇒ Object



174
175
176
# File 'lib/knuverse/knufactor/resource_collection.rb', line 174

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?



180
181
182
183
184
185
186
# File 'lib/knuverse/knufactor/resource_collection.rb', line 180

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:



135
136
137
138
139
140
141
# File 'lib/knuverse/knufactor/resource_collection.rb', line 135

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

#each(&block) ⇒ Object



21
22
23
# File 'lib/knuverse/knufactor/resource_collection.rb', line 21

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

#empty?Boolean

Does the collection contain anything?

Returns:

  • (Boolean)


27
28
29
# File 'lib/knuverse/knufactor/resource_collection.rb', line 27

def empty?
  @list.empty?
end

#first(n = nil) ⇒ ResourceCollection, Resource

Provide the first (or first ‘n`) entries

Parameters:

  • n (Fixnum) (defaults to: nil)

    How many to provide

Returns:



34
35
36
37
38
39
40
# File 'lib/knuverse/knufactor/resource_collection.rb', line 34

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

#last(n = nil) ⇒ ResourceCollection, Resource

Provide the last (or last ‘n`) entries

Parameters:

  • n (Fixnum) (defaults to: nil)

    How many to provide

Returns:



45
46
47
48
49
50
51
# File 'lib/knuverse/knufactor/resource_collection.rb', line 45

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

#merge(other) ⇒ ResourceCollection

Merge two collections

Parameters:

Returns:

Raises:



56
57
58
59
# File 'lib/knuverse/knufactor/resource_collection.rb', line 56

def merge(other)
  raise Exceptions::InvalidArguments unless other.is_a?(self.class)
  self + (other - self)
end

#modelObject

An alias for #type



62
63
64
# File 'lib/knuverse/knufactor/resource_collection.rb', line 62

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

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

Returns:

Raises:



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/knuverse/knufactor/resource_collection.rb', line 74

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) ⇒ Object

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



88
89
90
# File 'lib/knuverse/knufactor/resource_collection.rb', line 88

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

#sizeFixnum

Returns the number of Resource instances in the collection

Returns:

  • (Fixnum)


94
95
96
# File 'lib/knuverse/knufactor/resource_collection.rb', line 94

def size
  @list.size
end

#sort(&block) ⇒ ResourceCollection

Allow complex sorting like an Array

Returns:



100
101
102
# File 'lib/knuverse/knufactor/resource_collection.rb', line 100

def sort(&block)
  self.class.new(super(&block), type: @type, api_client: @api_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

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

Returns:

Raises:



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/knuverse/knufactor/resource_collection.rb', line 112

def where(attribute, value, options = {})
  valid_comparisons = [:'==', :'!=', :>, :'>=', :<, :'<=', :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