Skip to content
This repository was archived by the owner on Apr 17, 2018. It is now read-only.

Added serialization callbacks. #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/dm-serializer/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ def properties_to_serialize(options)
end
end
end

# add properties as a result from the serialization callback
def invoke_serialization_callback
serialization_callback_result = {}
if respond_to?(:serialization_callback)
serialization_callback_result = serialization_callback
unless serialization_callback_result.is_a?(Hash)
raise "#serialization_callback should return a Hash, found #{serialization_callback_result.class}"
end
end
serialization_callback_result
end

end

Model.append_inclusions(Serializer)
Expand Down
3 changes: 3 additions & 0 deletions lib/dm-serializer/to_csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def to_csv(*args)
row = properties_to_serialize(options).map do |property|
__send__(property.name).to_s
end
row += invoke_serialization_callback.map do |key, value|
value.to_s
end
csv << row
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/dm-serializer/to_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def as_json(options = {})
result[method] = __send__(method)
end

result.merge! invoke_serialization_callback

# Note: if you want to include a whole other model via relation, use
# :methods:
#
Expand Down
9 changes: 9 additions & 0 deletions lib/dm-serializer/to_xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ def to_xml_document(opts={}, doc = nil)
end
end

invoke_serialization_callback.each do |key, value|
xml_key_name = key.to_s.gsub(/[^a-z0-9_]/, '')
if value.respond_to?(:to_xml_document)
xml.add_xml(root, value.to_xml_document)
else
xml.add_node(root, xml_key_name, value.to_s)
end
end

doc
end

Expand Down
4 changes: 4 additions & 0 deletions lib/dm-serializer/to_yaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ def encode_with(coder, options = {})
value = __send__(method)
coder.add(method, value.is_a?(Class) ? value.to_s : value)
end

invoke_serialization_callback.each do |key, value|
coder.add(key.to_s, value.is_a?(Class) ? value.to_s : value)
end
end

private
Expand Down
19 changes: 19 additions & 0 deletions spec/public/to_csv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,27 @@
george.to_csv.should match(/false/)
end
end
end
end

describe DataMapper::Serialize, '#to_csv' do

it "supports serialization_callbacks" do
class Horse
include DataMapper::Resource

property :id, Serial
property :name, String

def serialization_callback
{ :external_name => "#{name}_external" }
end
end

horse = Horse.new(:name => 'legolas')
horse.to_csv.should match(/legolas_external/)
end

end
else
warn "[WARNING] Cannot require 'faster_csv' or 'csv', not running #to_csv specs"
Expand Down
35 changes: 35 additions & 0 deletions spec/public/to_json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,38 @@ def deserialize(result)
Motorcycle.new.as_json[:type].should == "Motorcycle"
end
end

describe DataMapper::Serializer, '#as_json' do
before(:each) do
class Horse
include DataMapper::Resource

property :id, Serial
property :name, String

def serialization_callback
{ :external_name => "#{name}_external" }
end
end
end

it "supports serialization_callbacks" do
horse = Horse.new(:name => 'legolas')
horse.as_json[:external_name].should == 'legolas_external'
end

it "should raise an exception when the serialization_callback does not return a Hash" do
class Horse
def serialization_callback
name
end
end

expect { Horse.new(:name => 'legolas').as_json }.to raise_error
end

it "should not raise an exception when the serialization_callback returns a Hash" do
expect { Horse.new(:name => 'legolas').as_json }.to_not raise_error
end

end
19 changes: 19 additions & 0 deletions spec/public/to_xml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,23 @@ def cast(value, type)
end
end
end

describe "adding properties from serialization_callback as well" do
it "should call the serialization_callback method and add the results" do
class Horse
include DataMapper::Resource

property :id, Serial
property :name, String

def serialization_callback
{ :external_name => "#{name}_external" }
end
end

horse = Horse.create(:name => 'legolas')
result = horse.to_xml
puts REXML::Document.new(result).elements[1].elements["external_name"].text == 'legolas_external'
end
end
end
16 changes: 16 additions & 0 deletions spec/public/to_yaml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,20 @@ def deserialize(result)
result[0]['name'].should == 'Berta'
end

it "supports serialization_callbacks" do
class Horse
include DataMapper::Resource

property :id, Serial
property :name, String

def serialization_callback
{ :external_name => "#{name}_external" }
end
end

horse = Horse.create(:name => 'legolas')
result = @harness.deserialize(YAML.dump(horse))
result['external_name'].should == 'legolas_external'
end
end