class Tickethub::Collection
Constants
- DEFAULT_LIMIT
- DEFAULT_OFFSET
Attributes
cache[RW]
count[R]
endpoint[R]
params[R]
Public Class Methods
new(endpoint, klass, params = {}, options = {}, cache = nil)
click to toggle source
Calls superclass method
# File lib/tickethub/collection.rb, line 14 def initialize(endpoint, klass, params = {}, options = {}, cache = nil) @params = params.dup @options = options.dup @endpoint = endpoint @klass = klass @cache = cache klass.registered_types.each do |type, options| define_singleton_method type do instance_variable_defined?("@#{type}") ? instance_variable_get("@#{type}") : instance_variable_set("@#{type}", Tickethub::Collection.new(endpoint[type], options[:klass])) end end klass.collection_methods.merge(klass.scopes).each do |key, block| define_singleton_method key, &block end super() do |yielder| self.reload! if self.cache.nil? self.cache.each do |row| yielder << @klass.call(endpoint, row) end while (offset + self.cache.length) < count response = endpoint.get params.merge(offset: self.cache.length) response.decoded.each do |row| self.cache << row yielder << @klass.call(endpoint, row) end end end end
Public Instance Methods
[](search)
click to toggle source
# File lib/tickethub/collection.rb, line 137 def [](search) case search when Fixnum self.offset(search).first when Hash self.filter(search).first when Range self.offset(search.min).first(search.max) when Array self.filter(id: search) when String @klass.call endpoint, CGI::escape(search), @options, @params else raise ArgumentError, 'invalid search value type' end end
aggregate(*group)
click to toggle source
# File lib/tickethub/collection.rb, line 154 def aggregate(*group) Tickethub::Aggregate.new self, group end
any?(&block)
click to toggle source
Calls superclass method
# File lib/tickethub/collection.rb, line 128 def any?(&block) block_given?? super : ! empty? end
create(attributes = {})
click to toggle source
# File lib/tickethub/collection.rb, line 158 def create(attributes = {}) @klass.call endpoint, post(attributes), @options rescue Tickethub::ResourceInvalid => err @klass.call endpoint, Tickethub::Response.new(err.response).decoded, @options end
empty?()
click to toggle source
# File lib/tickethub/collection.rb, line 124 def empty? count.zero? end
filter(value)
click to toggle source
# File lib/tickethub/collection.rb, line 79 def filter(value) self.class.new endpoint, @klass, params.merge(filters: filters.merge(value)) end
filters()
click to toggle source
# File lib/tickethub/collection.rb, line 84 def filters (params[:filters] || {}).dup end
last()
click to toggle source
# File lib/tickethub/collection.rb, line 57 def last offset(count - 1).first end
limit(value = nil)
click to toggle source
# File lib/tickethub/collection.rb, line 61 def limit(value = nil) if value.nil? return (@limit || params[:limit] || DEFAULT_LIMIT).to_i else self.class.new endpoint, @klass, params.merge(limit: value) end end
offset(value = nil)
click to toggle source
# File lib/tickethub/collection.rb, line 70 def offset(value = nil) if value.nil? return (@offset || params[:offset] || DEFAULT_OFFSET).to_i else self.class.new endpoint, @klass, params.merge(offset: value) end end
order(value = nil)
click to toggle source
# File lib/tickethub/collection.rb, line 92 def order(value = nil) return params[:order].dup if value.nil? order = (params[:order] || []) + (case value when Symbol, String then [value.to_s] when Hash value.collect do |key, direction| direction.to_s == 'desc' ? "-#{key}" : key end end) self.class.new endpoint, @klass, params.merge(order: order) end
reload!()
click to toggle source
# File lib/tickethub/collection.rb, line 48 def reload! @cache = (response = endpoint.get params).decoded @count, @offset, @limit = response.status == 206 ? response.headers.values_at(*%w(x-total-count x-offset x-limit)) .collect { |value| value[0].to_i } : [@cache.length, 0, @cache.length] return self end
scope(path, params = {})
click to toggle source
# File lib/tickethub/collection.rb, line 106 def scope(path, params = {}) self.class.new endpoint[path], @klass, self.params.merge(params) end
search(value)
click to toggle source
# File lib/tickethub/collection.rb, line 88 def search(value) self.filter(search: value) end
update(attributes)
click to toggle source
# File lib/tickethub/collection.rb, line 164 def update(attributes) self.patch attributes return true rescue Tickethub::ResourceInvalid => err @klass.call endpoint, Tickethub::Response.new(err.response).decoded, @options return false end