lib/restful_model_collection.rb in inbox-0.3.0 vs lib/restful_model_collection.rb in inbox-0.4.0
- old
+ new
@@ -3,15 +3,15 @@
module Inbox
class RestfulModelCollection
attr_accessor :filters
- def initialize(model_class, api, namespace, filters = {})
+ def initialize(model_class, api, namespace_id, filters = {})
raise StandardError.new unless api.class <= Inbox::API
@model_class = model_class
@filters = filters
- @namespace = namespace
+ @namespace_id = namespace_id
@_api = api
end
def each
offset = 0
@@ -34,11 +34,12 @@
range(0, Float::INFINITY)
end
def where(filters)
collection = self.clone
- collection.filters = filters
+ collection.filters ||= {}
+ collection.filters.merge!(filters)
collection
end
def range(offset = 0, limit = 50)
accumulated = []
@@ -69,11 +70,11 @@
def build(args)
for key in args.keys
args[key.to_s] = args[key]
end
- model = @model_class.new(@_api, @namespace)
+ model = @model_class.new(@_api, @namespace_id)
model.inflate(args)
model
end
def inflate_collection(items = [])
@@ -93,11 +94,11 @@
end
private
def url
- prefix = "/n/#{@namespace}" if @namespace
+ prefix = "/n/#{@namespace_id}" if @namespace_id
@_api.url_for_path("#{prefix}/#{@model_class.collection_name}")
end
def get_model(id)
model = nil
@@ -118,10 +119,10 @@
filters = @filters.clone
filters[:offset] = offset
filters[:limit] = limit
models = []
- RestClient.get("#{url}?#{filters.to_query}"){ |response,request,result|
+ RestClient.get(url, :params => filters){ |response,request,result|
items = Inbox.interpret_response(result, response, {:expected_class => Array})
models = inflate_collection(items)
}
models
end
\ No newline at end of file