Permalink
Browse files

Make the test pass for importing

  • Loading branch information...
jhchabran committed Jul 21, 2012
1 parent d1bc0fd commit 031f47d099c19ae4c7a222d97b3a075380b71214
Showing with 82 additions and 74 deletions.
  1. +37 −0 lib/vintner/builder.rb
  2. +35 −0 lib/vintner/importer.rb
  3. +2 −2 lib/vintner/property.rb
  4. +6 −71 lib/vintner/representation.rb
  5. +2 −1 spec/vintner/representer_spec.rb
View
@@ -0,0 +1,37 @@
+module Vintner
+ class Builder
+ def initialize &block
+ @store = {}
+ @block = block
+ end
+
+ def method_missing method_id, *args, &block
+ @store[method_id] = self.class.new(&block)
+ end
+
+ def property name
+ @store[name] = @representer.properties[name.to_sym].export(@model)
+ 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
+ end
+end
View
@@ -0,0 +1,35 @@
+module Vintner
+ class Importer
+ def initialize &block
+ @store = {}
+ @block = block
+ end
+
+ def method_missing method_id, *args, &block
+ @store[method_id] = self.class.new(&block)
+ end
+
+ def property name
+ @store[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
+
+ # 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]
+ else
+ importer_or_property.import model, hash[key.to_s]
+ end
+ end
+
+ hash
+ end
+ end
+end
View
@@ -1,6 +1,6 @@
module Vintner
class Property
- attr_reader :name
+ attr_reader :name, :getter, :setter
def initialize name, &block
@name = name
@@ -10,7 +10,7 @@ def initialize name, &block
def import model, value
if setter_defined?
- @setter.call mode, value
+ @setter.call model, value
else
model.send "#{name}=", value
end
@@ -1,85 +1,20 @@
+require 'vintner/builder'
+require 'vintner/importer'
+
module Vintner
class Representation
def initialize &block
@builder = Builder.new &block
+ @importer = Importer.new &block
end
def export representer, model
@builder.export representer, model
end
def import representer, model, hash
- @builder.import representer, model, hash
- end
- end
-
- 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]).import(model, value)
- 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
-
- # 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)
+ @importer.import representer, model, hash
+ model
end
end
end
@@ -76,7 +76,8 @@ class Dummy
end
it "should import json" do
- model = Struct.new(:formatted_title)
+ hash = {meta:{title:"some title"}}
+ model = Struct.new(:formatted_title).new
Dummy.import(model, hash.to_json).formatted_title.should ==("some title")
end

0 comments on commit 031f47d

Please sign in to comment.