Permalink
Browse files

Draf importer

  • Loading branch information...
1 parent ee6800a commit e03574d94097185189d2d3eacfb78165f8bd03cf @jhchabran jhchabran committed Jul 20, 2012
Showing with 48 additions and 6 deletions.
  1. +43 −5 lib/vintner/representation.rb
  2. +4 −0 lib/vintner/representer.rb
  3. +1 −1 spec/vintner/representer_spec.rb
@@ -7,14 +7,56 @@ def initialize &block
def export representer, model
@builder.export representer, model
end
+
+ def import representer, model, hash
+ @builder.import representer, model, hash
+ end
end
- class Builder
+ module DSLmethods
def initialize &block
@store = {}
@block = block
end
+ def method_missing method_id, *args, &block
+ @store[method_id] = self.class.new(&block)
+ end
+ end
+
+ class Importer
+ include DSLmethods
+
+ def property name
+ @representer.properties([name.to_sym])
+ end
+
+ def import representer, model, hash
+ @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.import(representer, model, hash)
+ else
+ hash[key] = builder
+ end
+ end
+
+ hash
+ end
+
+ end
+
+ class Builder
+ include DSLmethods
+
def export representer, model
@representer = representer
@model = model
@@ -39,9 +81,5 @@ def export representer, model
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
@@ -29,6 +29,10 @@ def representation &block
def export model
@representation.export(self, model).to_json
end
+
+ def import model, json
+ @representation.import(self, model, ActiveSupport::JSON.decode(json))
+ end
end
end
end
@@ -78,7 +78,7 @@ class Dummy
it "should import json" do
model = Struct.new(:formatted_title)
- Dummy.import(model, hash.to_json).title.should ==("some title")
+ Dummy.import(model, hash.to_json).formatted_title.should ==("some title")
end
end
end

0 comments on commit e03574d

Please sign in to comment.