Permalink
Browse files

Add export support for collections

  • Loading branch information...
1 parent b783991 commit 1451708d001f8a1811d9ff54d0efc9758373b44c @jhchabran jhchabran committed Jul 23, 2012
Showing with 110 additions and 58 deletions.
  1. +48 −0 lib/vintner/collection.rb
  2. +4 −0 lib/vintner/dsl_methods.rb
  3. +58 −58 spec/vintner/representer_spec.rb
@@ -0,0 +1,48 @@
+module Vintner
+ class Collection
+ def initialize name, representer, &block
+ @name = name
+ @representer = representer
+
+ instance_eval &block if block_given?
+ end
+
+ def import
+ raise "TODO"
+ end
+
+ def export collection
+ if getter_defined?
+ result = @getter.call collection
+ else
+ result = collection
+ end
+
+ wrap_representers result
+ end
+
+ def wrap_representers collection
+ collection.map do |item|
+ @representer.export(item)
+ end
+ end
+
+ def getter_defined?
+ !!@getter
+ end
+
+ def setter_defined?
+ !!@setter
+ end
+
+ private
+ def get &block
+ @getter = block
+ end
+
+ def set &block
+ @setter = block
+ end
+ end
+end
+
@@ -16,5 +16,9 @@ def method_missing method_id, *args, &block
def property name
@store[name] = @representer.properties[name.to_sym]
end
+
+ def collection name
+ @store[name] = @representer.collections[name.to_sym]
+ end
end
end
@@ -177,62 +177,62 @@ class Dummy
end
end
-# describe "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::Representer
-#
-# collection :dummies, Dummy do
-# get { |collection| collection }
-# end
-#
-# representation do |json|
-# json.meta do |meta|
-# meta.page 0
-# meta.total_pages 4
-# end
-#
-# json.collection :dummies
-# end
-# end
-#
-# @a = {:meta=>{:version => 4, :title => "test", :stuff => "stuff"}}
-# @b = {:meta=>{:version => 4, :title => "test2", :stuff => "stuff2"}}
-#
-# @model_klass = Struct.new(:formatted_title, :stuff)
-# @collection = [@model_klass.new("test", "stuff"), @model_klass.new("test2", "stuff2")]
-#
-# @hash = {
-# :meta => {
-# :page => 0,
-# :total_pages => 4
-# },
-# :dummies => [@a, @b]
-# }
-# end
-#
-# it "should export the collection" do
-# DummyCollection.export(@collection).should ==(@hash.to_json)
-# end
-# end
+ describe "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::Representer
+
+ collection :dummies, Dummy do
+ get { |collection| collection }
+ end
+
+ representation do |json|
+ json.meta do |meta|
+ meta.page 0
+ meta.total_pages 4
+ end
+
+ json.collection :dummies
+ end
+ end
+
+ @a = {:meta=>{:title => "test"}}
+ @b = {:meta=>{:title => "test2"}}
+
+ @model_klass = Struct.new(:formatted_title, :stuff)
+ @collection = [@model_klass.new("test"), @model_klass.new("test2")]
+
+ @hash = {
+ :meta => {
+ :page => 0,
+ :total_pages => 4
+ },
+ :dummies => [@a, @b]
+ }
+ end
+
+ it "should export the collection" do
+ DummyCollection.new(@collection).to_json.should ==(@hash.to_json)
+ end
+ end
end

0 comments on commit 1451708

Please sign in to comment.