Permalink
Browse files

Add representation

  • Loading branch information...
1 parent c86766e commit 61bb52d7fe85ffa5fcc78929d86732ddacec5567 @jhchabran jhchabran committed Jul 20, 2012
Showing with 88 additions and 3 deletions.
  1. +1 −0 lib/vintner.rb
  2. +47 −0 lib/vintner/representation.rb
  3. +6 −0 lib/vintner/representer.rb
  4. +34 −3 spec/vintner/representer_spec.rb
View
@@ -1,5 +1,6 @@
require "vintner/version"
require "vintner/property"
+require "vintner/representation"
require "vintner/representer"
module Vintner
@@ -0,0 +1,47 @@
+module Vintner
+ class Representation
+ def initialize &block
+ @builder = Builder.new &block
+ end
+
+ def export representer, model
+ @builder.export representer, model
+ end
+ end
+
+ class Builder
+ def initialize &block
+ @store = {}
+ @block = block
+ end
+
+ def export representer, model
+ @representer = representer
+ @model = model
+
+ # Registering keys and properties
+ @block.call(self) if @block
+
+ hash = {}
+
+ # Then we play the score accordingly
+ @store.each do |key, builder|
+ if builder.is_a? Builder
+ hash[key] = builder.export(representer, model)
+ else
+ hash[key] = builder
+ end
+ end
+
+ hash
+ end
+
+ def property name
+ @store[name] = @representer.properties[name.to_sym].export(@model)
+ end
+
+ def method_missing method_id, *args, &block
+ @store[method_id] = self.class.new(&block)
+ end
+ end
+end
@@ -21,7 +21,13 @@ def collection name, representer
end
def representation &block
+ return @representation unless block_given?
+ @representation = Representation.new &block
+ end
+
+ def export model
+ @representation.export(self, model).to_json
end
end
end
@@ -2,7 +2,7 @@
module Vintner
describe Representer do
- it "should allow to declare a simple property" do
+ it "should declare a simple property" do
class Dummy
include Vintner::Representer
@@ -12,7 +12,7 @@ class Dummy
Dummy.properties.should include(:title)
end
- it "should allow to declare a property with a getter" do
+ it "should declare a property with a getter" do
class Dummy
include Vintner::Representer
@@ -27,7 +27,7 @@ class Dummy
Dummy.properties[:title].getter_defined?.should be(true)
end
- it "should allow to declare a property with a getter" do
+ it "should declare a property with a setter" do
class Dummy
include Vintner::Representer
@@ -40,5 +40,36 @@ class Dummy
Dummy.properties[:title].setter_defined?.should be(true)
end
+
+ describe "Representation" do
+ before :each do
+ class Dummy
+ include Vintner::Representer
+
+ property :title do
+ set do |model, value|
+ model.formatted_title = value
+ end
+ end
+
+ representation do |json|
+ json.meta do |meta|
+ meta.property :title
+ end
+ end
+ end
+ end
+
+ it "should declare a representation" do
+ Dummy.representation.should_not be_nil
+ end
+
+ it "should export json" do
+ hash = {meta:{title:"some title"}}
+
+ model = Struct.new(:title).new("some title")
+ Dummy.export(model).should ==(hash.to_json)
+ end
+ end
end
end

0 comments on commit 61bb52d

Please sign in to comment.