Class: Tickethub::Collection
- Inherits:
-
Enumerator
- Object
- Enumerator
- Tickethub::Collection
- Defined in:
- lib/tickethub/collection.rb
Constant Summary collapse
- DEFAULT_LIMIT =
25.freeze
- DEFAULT_OFFSET =
0.freeze
Instance Attribute Summary collapse
-
#cache ⇒ Object
Returns the value of attribute cache.
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
- #[](search) ⇒ Object
- #aggregate(*group) ⇒ Object
- #any?(&block) ⇒ Boolean
- #create(attributes = {}) ⇒ Object
- #empty? ⇒ Boolean
- #filter(value) ⇒ Object
- #filters ⇒ Object
-
#initialize(endpoint, klass, params = {}, options = {}, cache = nil) ⇒ Collection
constructor
A new instance of Collection.
- #last ⇒ Object
- #limit(value = nil) ⇒ Object
- #offset(value = nil) ⇒ Object
- #order(value = nil) ⇒ Object
- #reload! ⇒ Object
- #scope(path, params = {}) ⇒ Object
- #search(value) ⇒ Object
- #update(attributes) ⇒ Object
Constructor Details
#initialize(endpoint, klass, params = {}, options = {}, cache = nil) ⇒ Collection
Returns a new instance of Collection.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/tickethub/collection.rb', line 14 def initialize(endpoint, klass, params = {}, = {}, cache = nil) @params = params.dup @options = .dup @endpoint = endpoint @klass = klass @cache = cache klass.registered_types.each do |type, | define_singleton_method type do instance_variable_defined?("@#{type}") ? instance_variable_get("@#{type}") : instance_variable_set("@#{type}", Tickethub::Collection.new(endpoint[type], [: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 |
Instance Attribute Details
#cache ⇒ Object
Returns the value of attribute cache.
11 12 13 |
# File 'lib/tickethub/collection.rb', line 11 def cache @cache end |
#count ⇒ Object (readonly)
Returns the value of attribute count.
12 13 14 |
# File 'lib/tickethub/collection.rb', line 12 def count @count end |
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
12 13 14 |
# File 'lib/tickethub/collection.rb', line 12 def endpoint @endpoint end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
12 13 14 |
# File 'lib/tickethub/collection.rb', line 12 def params @params end |
Instance Method Details
#[](search) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# 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) ⇒ Object
154 155 156 |
# File 'lib/tickethub/collection.rb', line 154 def aggregate(*group) Tickethub::Aggregate.new self, group end |
#any?(&block) ⇒ Boolean
128 129 130 |
# File 'lib/tickethub/collection.rb', line 128 def any?(&block) block_given?? super : ! empty? end |
#create(attributes = {}) ⇒ Object
158 159 160 161 162 |
# 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? ⇒ Boolean
124 125 126 |
# File 'lib/tickethub/collection.rb', line 124 def empty? count.zero? end |
#filter(value) ⇒ Object
79 80 81 82 |
# File 'lib/tickethub/collection.rb', line 79 def filter(value) self.class.new endpoint, @klass, params.merge(filters: filters.merge(value)) end |
#filters ⇒ Object
84 85 86 |
# File 'lib/tickethub/collection.rb', line 84 def filters (params[:filters] || {}).dup end |
#last ⇒ Object
57 58 59 |
# File 'lib/tickethub/collection.rb', line 57 def last offset(count - 1).first end |
#limit(value = nil) ⇒ Object
61 62 63 64 65 66 67 68 |
# 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) ⇒ Object
70 71 72 73 74 75 76 77 |
# 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) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# 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! ⇒ Object
48 49 50 51 52 53 54 55 |
# 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 = {}) ⇒ Object
106 107 108 109 |
# File 'lib/tickethub/collection.rb', line 106 def scope(path, params = {}) self.class.new endpoint[path], @klass, self.params.merge(params) end |
#search(value) ⇒ Object
88 89 90 |
# File 'lib/tickethub/collection.rb', line 88 def search(value) self.filter(search: value) end |