Skip to content

Commit

Permalink
Add standalone collections
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchabran committed Jul 23, 2012
1 parent c22a87e commit 4ace812
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/vintner.rb
Expand Up @@ -3,6 +3,7 @@
require "vintner/collection"
require "vintner/representation"
require "vintner/representer"
require "vintner/collection_representer"

module Vintner
# Your code goes here...
Expand Down
1 change: 0 additions & 1 deletion lib/vintner/builder.rb
Expand Up @@ -21,7 +21,6 @@ def export representer, model
else
hash[key] = object
end

end
end

Expand Down
43 changes: 43 additions & 0 deletions lib/vintner/collection_representer.rb
@@ -0,0 +1,43 @@
require 'active_support/concern'
require 'active_support/core_ext'

module Vintner
module CollectionRepresenter
extend ::ActiveSupport::Concern

def initialize collection
@collection = collection
end

def to_json
self.class.export(@collection).to_json
end


module ClassMethods
mattr_reader :collection

def representer representer_klass
@@collection = Collection.new(name, representer_klass)
end

def representation &block
return @representation unless block_given?

@representation = Representation.new &block
end

def export collection
@representation.export(self, collection)
end

def represents_model?
false
end

def represents_collection?
true
end
end
end
end
6 changes: 5 additions & 1 deletion lib/vintner/dsl_methods.rb
Expand Up @@ -18,7 +18,11 @@ def property name
end

def collection name
@store[name] = @representer.collections[name.to_sym]
if @representer.represents_model?
@store[name] = @representer.collections[name.to_sym]
else
@store[name] = @representer.collection
end
end
end
end
11 changes: 9 additions & 2 deletions lib/vintner/representer.rb
Expand Up @@ -26,10 +26,10 @@ def property name, &block
@@properties[name] = Property.new(name, &block)
end

def collection name, representer
def collection name, representer, &block
@@collections ||= {}

@@collections[name] = Collection.new(name, representer)
@@collections[name] = Collection.new(name, representer, &block)
end

def representation &block
Expand All @@ -46,6 +46,13 @@ def import model, hash
@representation.import(self, model, hash)
end

def represents_model?
true
end

def represents_collection?
false
end
end
end
end
67 changes: 62 additions & 5 deletions spec/vintner/representer_spec.rb
Expand Up @@ -177,7 +177,7 @@ class Dummy
end
end

describe "Collections" do
describe "Nested collections" do
before :each do
class Dummy
include Vintner::Representer
Expand All @@ -199,10 +199,10 @@ class Dummy
end
end

class DummyCollection
class NestedDummies
include Vintner::Representer

collection :dummies, Dummy do
collection :users, Dummy do
get { |model| model.dummies }
end

Expand All @@ -212,7 +212,7 @@ class DummyCollection
meta.total_pages 4
end

json.collection :dummies
json.collection :users
end
end

Expand All @@ -228,7 +228,64 @@ class DummyCollection
:page => 0,
:total_pages => 4
},
:dummies => [@a, @b]
:users => [@a, @b]
}
end

it "should export the collection" do
NestedDummies.new(@collection).to_json.should ==(@hash.to_json)
end
end

describe "Standalone collections" do
before :each do
class Dummy
include Vintner::Representer

property :title do
get do |model|
model.formatted_title
end

set do |model, value|
model.formatted_title = value
end
end

representation do |json|
json.meta do |meta|
meta.property :title
end
end
end

class DummyCollection
include Vintner::CollectionRepresenter

representer Dummy

representation do |json|
json.meta do |meta|
meta.page 0
meta.total_pages 4
end

json.collection :users
end
end

@a = {:meta=>{:title => "test"}}
@b = {:meta=>{:title => "test2"}}

@model_klass = Struct.new(:formatted_title)
@collection = [@model_klass.new("test"), @model_klass.new("test2")]

@hash = {
:meta => {
:page => 0,
:total_pages => 4
},
:users => [@a, @b]
}
end

Expand Down

0 comments on commit 4ace812

Please sign in to comment.