Class: ActiveResource::Collection
- Inherits:
-
Object
- Object
- ActiveResource::Collection
- Includes:
- Enumerable
- Defined in:
- lib/active_resource/collection.rb
Overview
:nodoc:
Constant Summary collapse
- SELF_DEFINE_METHODS =
[:to_a, :collect!, :map!, :all?]
Instance Attribute Summary collapse
-
#elements ⇒ Object
The array of actual elements returned by index actions.
-
#original_params ⇒ Object
The array of actual elements returned by index actions.
-
#resource_class ⇒ Object
The array of actual elements returned by index actions.
Instance Method Summary collapse
- #collect! ⇒ Object (also: #map!)
- #first_or_create(attributes = {}) ⇒ Object
- #first_or_initialize(attributes = {}) ⇒ Object
-
#initialize(elements = []) ⇒ Collection
constructor
ActiveResource::Collection is a wrapper to handle parsing index responses that do not directly map to Rails conventions.
- #to_a ⇒ Object
- #where(clauses = {}) ⇒ Object
Constructor Details
#initialize(elements = []) ⇒ Collection
ActiveResource::Collection is a wrapper to handle parsing index responses that do not directly map to Rails conventions.
You can define a custom class that inherits from ActiveResource::Collection in order to to set the elements instance.
GET /posts.json delivers following response body:
{
posts: [
{
title: "ActiveResource now has associations",
body: "Lorem Ipsum"
},
{...}
],
next_page: "/posts.json?page=2"
}
A Post class can be setup to handle it with:
class Post < ActiveResource::Base
self.site = "http://example.com"
self.collection_parser = PostCollection
end
And the collection parser:
class PostCollection < ActiveResource::Collection
attr_accessor :next_page
def initialize(parsed = {})
@elements = parsed['posts']
@next_page = parsed['next_page']
end
end
The result from a find method that returns multiple entries will now be a PostParser instance. ActiveResource::Collection includes Enumerable and instances can be iterated over just like an array.
@posts = Post.find(:all) # => PostCollection:xxx
@posts.next_page # => "/posts.json?page=2"
@posts.map(&:id) # =>[1, 3, 5 ...]
The initialize method will receive the ActiveResource::Formats parsed result and should set @elements.
59 60 61 |
# File 'lib/active_resource/collection.rb', line 59 def initialize(elements = []) @elements = elements end |
Instance Attribute Details
#elements ⇒ Object
The array of actual elements returned by index actions
13 14 15 |
# File 'lib/active_resource/collection.rb', line 13 def elements @elements end |
#original_params ⇒ Object
The array of actual elements returned by index actions
13 14 15 |
# File 'lib/active_resource/collection.rb', line 13 def original_params @original_params end |
#resource_class ⇒ Object
The array of actual elements returned by index actions
13 14 15 |
# File 'lib/active_resource/collection.rb', line 13 def resource_class @resource_class end |
Instance Method Details
#collect! ⇒ Object Also known as: map!
67 68 69 70 71 72 73 |
# File 'lib/active_resource/collection.rb', line 67 def collect! return elements unless block_given? set = [] each { |o| set << yield(o) } @elements = set self end |
#first_or_create(attributes = {}) ⇒ Object
76 77 78 79 80 |
# File 'lib/active_resource/collection.rb', line 76 def first_or_create(attributes = {}) first || resource_class.create(original_params.update(attributes)) rescue NoMethodError raise "Cannot create resource from resource type: #{resource_class.inspect}" end |
#first_or_initialize(attributes = {}) ⇒ Object
82 83 84 85 86 |
# File 'lib/active_resource/collection.rb', line 82 def first_or_initialize(attributes = {}) first || resource_class.new(original_params.update(attributes)) rescue NoMethodError raise "Cannot build resource from resource type: #{resource_class.inspect}" end |
#to_a ⇒ Object
63 64 65 |
# File 'lib/active_resource/collection.rb', line 63 def to_a elements end |
#where(clauses = {}) ⇒ Object
88 89 90 91 92 |
# File 'lib/active_resource/collection.rb', line 88 def where(clauses = {}) raise ArgumentError, "expected a clauses Hash, got #{clauses.inspect}" unless clauses.is_a? Hash new_clauses = original_params.merge(clauses) resource_class.where(new_clauses) end |