Permalink
Browse files

use rufus-json

  • Loading branch information...
1 parent 64a3262 commit 3695a79e4a79bdca4a0653e61e9693f749bc6013 @jamesgolick committed Jul 1, 2010
Showing with 9 additions and 7 deletions.
  1. +5 −3 lib/friendly/translator.rb
  2. +4 −4 spec/unit/translator_spec.rb
View
8 lib/friendly/translator.rb
@@ -1,17 +1,19 @@
+require "rufus-json"
+
module Friendly
class Translator
RESERVED_ATTRS = [:id, :created_at, :updated_at].freeze
attr_reader :serializer, :time
- def initialize(serializer = JSON, time = Time)
+ def initialize(serializer = Rufus::Json, time = Time)
@serializer = serializer
@time = time
end
def to_object(klass, record)
record.delete(:added_id)
- attributes = serializer.parse(record.delete(:attributes))
+ attributes = serializer.decode(record.delete(:attributes))
attributes.merge!(record).merge!(:new_record => false)
klass.new_without_change_tracking attributes
end
@@ -26,7 +28,7 @@ def to_record(document)
protected
def serialize(document)
attrs = document.to_hash.reject { |k,v| RESERVED_ATTRS.include?(k) }
- serializer.generate(attrs)
+ serializer.encode(attrs)
end
end
end
View
8 spec/unit/translator_spec.rb
@@ -10,7 +10,7 @@
describe "translating a row to an object" do
before do
- @serializer.stubs(:parse).with("THE JSON").returns(:name => "Stewie")
+ @serializer.stubs(:decode).with("THE JSON").returns(:name => "Stewie")
@time = Time.new
@row = {:added_id => 12345,
:created_at => @time,
@@ -33,7 +33,7 @@
before do
@hash = {:name => "Stewie"}
@time.stubs(:new).returns(Time.new)
- @serializer.stubs(:generate).with(@hash).returns("SOME JSON")
+ @serializer.stubs(:encode).with(@hash).returns("SOME JSON")
@document = stub(:to_hash => @hash,
:new_record? => true,
:created_at => nil,
@@ -62,7 +62,7 @@
:created_at => @created_at,
:updated_at => Time.new}
@time.stubs(:new).returns(Time.new + 5000)
- @serializer.stubs(:generate).returns("SOME JSON")
+ @serializer.stubs(:encode).returns("SOME JSON")
@document = stub(:to_hash => @hash,
:created_at => @created_at,
:new_record? => false,
@@ -71,7 +71,7 @@
end
it "serializes the attributes" do
- @serializer.should have_received(:generate).with(:name => "Stewie")
+ @serializer.should have_received(:encode).with(:name => "Stewie")
@record[:attributes].should == "SOME JSON"
end

0 comments on commit 3695a79

Please sign in to comment.