Class: KnuVerse::Knufactor::ResourceCollection
- Inherits:
-
Object
- Object
- KnuVerse::Knufactor::ResourceCollection
- 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
-
#type ⇒ Class
readonly
A collection of this Resource subclass.
Instance Method Summary collapse
-
#+(other) ⇒ ResourceCollection
Return a collection after adding to the original Warning: this may cause duplicates or mixed type joins! For safety, use #merge.
-
#-(other) ⇒ ResourceCollection
Return a collection after subtracting from the original.
- #<<(other) ⇒ Object
- #<=>(other) ⇒ Object
-
#==(other) ⇒ Boolean
Allow comparison of collection.
-
#[](index) ⇒ Resource, ResourceCollection
Return the collection item at the specified index.
- #each(&block) ⇒ Object
-
#empty? ⇒ Boolean
Does the collection contain anything?.
-
#first(n = nil) ⇒ ResourceCollection, Resource
Provide the first (or first ‘n`) entries.
-
#initialize(list, options = {}) ⇒ ResourceCollection
constructor
A new instance of ResourceCollection.
-
#last(n = nil) ⇒ ResourceCollection, Resource
Provide the last (or last ‘n`) entries.
-
#merge(other) ⇒ ResourceCollection
Merge two collections.
-
#model ⇒ Object
An alias for #type.
-
#or(attribute, value, options = {}) ⇒ ResourceCollection
Hacked together #or() method in the same spirit as #where().
-
#paginate(*args) ⇒ Object
Pass pagination through to the Array (which passes to will_paginate).
-
#size ⇒ Fixnum
Returns the number of Resource instances in the collection.
-
#sort(&block) ⇒ ResourceCollection
Allow complex sorting like an Array.
-
#where(attribute, value, options = {}) ⇒ ResourceCollection
(also: #and)
Horribly inefficient way to allow querying Resources by their attributes.
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, = {}) # TODO: better options validations raise Exceptions::InvalidOptions unless .is_a?(Hash) raise Exceptions::InvalidArguments if list.empty? && [:type].nil? @api_client = [:api_client] || APIClient.instance @list = list @type = [:type] || list.first.class end |
Instance Attribute Details
#type ⇒ Class (readonly)
Returns a collection of this KnuVerse::Knufactor::Resource subclass.
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
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
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
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
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?
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
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
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
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 |
#model ⇒ Object
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.
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, = {}) [:comparison] ||= value.is_a?(Regexp) ? :match : '==' if empty? @type.where(attribute, value, comparison: [:comparison], api_client: @api_client) else merge first.class.where( attribute, value, comparison: [: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 |
#size ⇒ Fixnum
Returns the number of Resource instances in the collection
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
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.
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, = {}) valid_comparisons = [:'==', :'!=', :>, :'>=', :<, :'<=', :match] [:comparison] ||= value.is_a?(Regexp) ? :match : '==' unless valid_comparisons.include?([: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([:comparison].to_sym, value) item end end.compact, type: @type, api_client: @api_client ) end |