Permalink
Browse files

Add support for export only values

  • Loading branch information...
1 parent b99f822 commit f2766c24ccda2acebbb140a95f0718dfaa2ead5e @jhchabran jhchabran committed Jul 22, 2012
Showing with 54 additions and 9 deletions.
  1. +9 −4 lib/vintner/builder.rb
  2. +5 −1 lib/vintner/dsl_methods.rb
  3. +4 −4 lib/vintner/importer.rb
  4. +36 −0 spec/vintner/representer_spec.rb
View
@@ -12,11 +12,16 @@ def export representer, model
hash = {}
# Then we play the score accordingly
- @store.each do |key, builder|
- if builder.is_a? Builder
- hash[key] = builder.export(representer, model)
+ @store.each do |key, object|
+ if object.is_a? Builder
+ hash[key] = object.export(representer, model)
else
- hash[key] = builder.export(@model)
+ if object.respond_to? :export
+ hash[key] = object.export(@model)
+ else
+ hash[key] = object
+ end
+
end
end
@@ -6,7 +6,11 @@ def initialize &block
end
def method_missing method_id, *args, &block
- @store[method_id] = self.class.new(&block)
+ if args.length > 0 && !block_given?
+ @store[method_id] = args.first
+ else
+ @store[method_id] = self.class.new(&block)
+ end
end
def property name
View
@@ -10,11 +10,11 @@ def import representer, model, hash
@block.call(self) if @block
# Then we play the score accordingly
- @store.each do |key, importer_or_property|
- if importer_or_property.is_a? Importer
- importer_or_property.import representer, model, hash[key.to_s]
+ @store.each do |key, object|
+ if object.is_a? Importer
+ object.import representer, model, hash[key.to_s]
else
- importer_or_property.import model, hash[key.to_s]
+ object.import model, hash[key.to_s] if object.respond_to? :import
end
end
@@ -138,5 +138,41 @@ class Dummy
Dummy.import(model, hash.to_json).position_y.should ==(5)
end
end
+
+ describe "Immediate values" do
+ before :each do
+ @hash = {:meta=>{:version => 4, :title => "test"}}
+ @model = Struct.new(:formatted_title).new("test")
+
+ class Dummy
+ include Vintner::Representer
+
+ property :title do
+ get do |model|
+ model.formatted_title
+ end
+
+ set do |model, title|
+ model.formatted_title = title
+ end
+ end
+
+ representation do |json|
+ json.meta do |meta|
+ meta.version 4
+ meta.property :title
+ end
+ end
+ end
+ end
+
+ it "should export immediate values " do
+ Dummy.export(@model).should ==(@hash.to_json)
+ end
+
+ it "should ignore it when importing" do
+ Dummy.import(@model, @hash.to_json).formatted_title.should ==(test)
+ end
+ end
end
end

0 comments on commit f2766c2

Please sign in to comment.