Class: RestfulModelCollection
- Inherits:
-
Object
- Object
- RestfulModelCollection
- Defined in:
- lib/restful_model_collection.rb
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, parent = nil) ⇒ RestfulModelCollection
constructor
A new instance of RestfulModelCollection.
- #path(id = "") ⇒ Object
- #range(offset = 0, count = 50) ⇒ Object
Constructor Details
#initialize(model_class, api, parent = nil) ⇒ RestfulModelCollection
Returns a new instance of RestfulModelCollection.
5 6 7 8 9 |
# File 'lib/restful_model_collection.rb', line 5 def initialize(model_class, api, parent = nil) @model_class = model_class @_parent = parent @_api = api end |
Instance Method Details
#all ⇒ Object
28 29 30 |
# File 'lib/restful_model_collection.rb', line 28 def all range(0, Float::INFINITY) end |
#build(*args) ⇒ Object
60 61 62 |
# File 'lib/restful_model_collection.rb', line 60 def build(*args) @model_class.new(self, *args) end |
#delete(item_or_id) ⇒ Object
49 50 51 52 53 |
# File 'lib/restful_model_collection.rb', line 49 def delete(item_or_id) item_or_id = item_or_id._id if item_or_id.is_a?(RestfulModel) url = @_api.url_for_path(self.path(item_or_id)) RestClient.delete(url) end |
#each ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/restful_model_collection.rb', line 11 def each offset = 0 finished = false while (!finished) do items = get_restful_model_collection(offset) break if items.length == 0 items.each { |item| yield item } offset += items.length end end |
#find(id) ⇒ Object
55 56 57 58 |
# File 'lib/restful_model_collection.rb', line 55 def find(id) return nil unless id get_restful_model(id) end |
#first ⇒ Object
24 25 26 |
# File 'lib/restful_model_collection.rb', line 24 def first get_restful_model_collection.first end |
#inflate_collection(items = []) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/restful_model_collection.rb', line 64 def inflate_collection(items = []) models = [] return unless items.is_a?(Array) items.each do |json| if @model_class < RestfulModel model = @model_class.new(self) model.inflate(json) else model = @model_class.new(json) end models.push(model) end models end |
#path(id = "") ⇒ Object
80 81 82 83 |
# File 'lib/restful_model_collection.rb', line 80 def path(id = "") prefix = @_parent ? @_parent.path : '' "#{prefix}/#{@model_class.collection_name}/#{id}" end |
#range(offset = 0, count = 50) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/restful_model_collection.rb', line 32 def range(offset = 0, count = 50) accumulated = [] finished = false chunk_size = 50 while (!finished && accumulated.length < count) do results = get_restful_model_collection(offset + accumulated.length, chunk_size) accumulated = accumulated.concat(results) # we're done if we have more than 'count' items, or if we asked for 50 and got less than 50... finished = accumulated.length >= count || results.length == 0 || (results.length % chunk_size != 0) end accumulated = accumulated[0..count] if count < Float::INFINITY accumulated end |