Permalink
Browse files

Added as_json, similar to to_json.

  • Loading branch information...
1 parent 04cf005 commit 8b777108ee90506df2f819f7a1e41f2e5b077374 @dblock dblock committed Aug 24, 2011
@@ -320,7 +320,7 @@ def size
end
alias :length :size
- # Need to specifically send #to_json to the entries.
+ # Send #to_json to the entries.
#
# @example Get the enumerable as json.
# enumerable.to_json
@@ -334,6 +334,20 @@ def to_json(options = {})
entries.to_json(options)
end
+ # Send #as_json to the entries, without encoding.
+ #
+ # @example Get the enumerable as json.
+ # enumerable.as_json
+ #
+ # @param [ Hash ] options Optional parameters.
+ #
+ # @return [ String ] The entries all loaded as a string.
+ #
+ # @since 2.2.0
+ def as_json(options = {})
+ entries.as_json(options)
+ end
+
# Return all the unique documents in the enumerable.
#
# @note This operation loads all documents from the database.
@@ -1220,6 +1220,62 @@
end
end
+ describe "#as_json" do
+
+ let(:person) do
+ Person.create(:ssn => "422-21-9687")
+ end
+
+ let!(:post) do
+ Post.create(:title => "test", :person_id => person.id)
+ end
+
+ let(:criteria) do
+ Post.where(:person_id => person.id)
+ end
+
+ let!(:enumerable) do
+ described_class.new(criteria)
+ end
+
+ before do
+ enumerable << post
+ end
+
+ let!(:json) do
+ enumerable.as_json
+ end
+
+ it "serializes the enumerable" do
+ json.size.should == 1
+ json[0]['title'].should == post.title
+ end
+ end
+
+ describe "#as_json(parameters)" do
+
+ let(:person) do
+ Person.create(:ssn => "422-21-9687")
+ end
+
+ let!(:post) do
+ Post.create(:title => "test", :person_id => person.id)
+ end
+
+ let(:criteria) do
+ Post.where(:person_id => person.id)
+ end
+
+ let!(:json) do
+ person.posts.as_json({:except => 'title'})
+ end
+
+ it "serializes the enumerable" do
+ json.size.should == 1
+ json[0].keys.should_not include('title')
+ end
+ end
+
describe "#uniq" do
let(:person) do

0 comments on commit 8b77710

Please sign in to comment.