Skip to content

Commit

Permalink
update Sawyer::Serializer to be configurable to other formats (like M…
Browse files Browse the repository at this point in the history
…essagePack)
  • Loading branch information
technoweenie committed Sep 28, 2012
1 parent 847cda4 commit e03f615
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/sawyer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ class Error < StandardError; end
resource
relation
response
serializer
agent
).each { |f| require File.expand_path("../sawyer/#{f}", __FILE__) }
1 change: 0 additions & 1 deletion lib/sawyer/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class << self

def self.serializer
@serializer ||= begin
require File.expand_path("../serializer", __FILE__)
require 'yajl'
Serializer.new(Yajl)
end
Expand Down
8 changes: 5 additions & 3 deletions lib/sawyer/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ class Serializer
# Public: Wraps a serialization format for Sawyer. Nested objects are
# prepared for serialization (such as changing Times to ISO 8601 Strings).
# Any serialization format that responds to #dump and #load will work.
def initialize(format)
def initialize(format, dump_method_name = nil, load_method_name = nil)
@format = format
@dump = @format.method(dump_method_name || :dump)
@load = @format.method(load_method_name || :load)
end

# Public: Encodes an Object (usually a Hash or Array of Hashes).
Expand All @@ -16,7 +18,7 @@ def initialize(format)
#
# Returns an encoded String.
def encode(data)
@format.dump(encode_object(data))
@dump.call(encode_object(data))
end

# Public: Decodes a String into an Object (usually a Hash or Array of
Expand All @@ -26,7 +28,7 @@ def encode(data)
#
# Returns a decoded Object.
def decode(data)
decode_object(@format.load(data))
decode_object(@load.call(data))
end

def encode_object(data)
Expand Down

0 comments on commit e03f615

Please sign in to comment.