Class: Inbox::RestfulModelCollection
- Inherits:
-
Object
- Object
- Inbox::RestfulModelCollection
- Defined in:
- lib/restful_model_collection.rb
Instance Attribute Summary collapse
-
#filters ⇒ Object
Returns the value of attribute filters.
Instance Method Summary collapse
- #all ⇒ Object
- #build(args) ⇒ Object
- #delete(item_or_id) ⇒ Object
- #each ⇒ Object
- #find(id) ⇒ Object
- #first ⇒ Object
- #inflate_collection(items = []) ⇒ Object
-
#initialize(model_class, api, namespace_id, filters = {}) ⇒ RestfulModelCollection
constructor
A new instance of RestfulModelCollection.
- #range(offset = 0, limit = 50) ⇒ Object
- #where(filters) ⇒ Object
Constructor Details
#initialize(model_class, api, namespace_id, filters = {}) ⇒ RestfulModelCollection
Returns a new instance of RestfulModelCollection.
8 9 10 11 12 13 14 |
# File 'lib/restful_model_collection.rb', line 8 def initialize(model_class, api, namespace_id, filters = {}) raise StandardError.new unless api.class <= Inbox::API @model_class = model_class @filters = filters @namespace_id = namespace_id @_api = api end |
Instance Attribute Details
#filters ⇒ Object
Returns the value of attribute filters.
6 7 8 |
# File 'lib/restful_model_collection.rb', line 6 def filters @filters end |
Instance Method Details
#all ⇒ Object
33 34 35 |
# File 'lib/restful_model_collection.rb', line 33 def all range(0, Float::INFINITY) end |
#build(args) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/restful_model_collection.rb', line 71 def build(args) for key in args.keys args[key.to_s] = args[key] end model = @model_class.new(@_api, @namespace_id) model.inflate(args) model end |
#delete(item_or_id) ⇒ Object
61 62 63 64 |
# File 'lib/restful_model_collection.rb', line 61 def delete(item_or_id) item_or_id = item_or_id.id if item_or_id.is_a?(RestfulModel) RestClient.delete("#{url}/#{id}") end |
#each ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/restful_model_collection.rb', line 16 def each offset = 0 finished = false while (!finished) do items = get_model_collection(offset) break if items.length == 0 items.each { |item| yield item } offset += items.length end end |
#find(id) ⇒ Object
66 67 68 69 |
# File 'lib/restful_model_collection.rb', line 66 def find(id) return nil unless id get_model(id) end |
#first ⇒ Object
29 30 31 |
# File 'lib/restful_model_collection.rb', line 29 def first get_model_collection.first end |
#inflate_collection(items = []) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/restful_model_collection.rb', line 80 def inflate_collection(items = []) models = [] return unless items.is_a?(Array) items.each do |json| if @model_class < RestfulModel model = @model_class.new(@_api) model.inflate(json) else model = @model_class.new(json) end models.push(model) end models end |
#range(offset = 0, limit = 50) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/restful_model_collection.rb', line 44 def range(offset = 0, limit = 50) accumulated = [] finished = false chunk_size = 50 while (!finished && accumulated.length < limit) do results = get_model_collection(offset + accumulated.length, chunk_size) accumulated = accumulated.concat(results) # we're done if we have more than 'limit' items, or if we asked for 50 and got less than 50... finished = accumulated.length >= limit || results.length == 0 || (results.length % chunk_size != 0) end accumulated = accumulated[0..limit] if limit < Float::INFINITY accumulated end |
#where(filters) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/restful_model_collection.rb', line 37 def where(filters) collection = self.clone collection.filters ||= {} collection.filters.merge!(filters) collection end |