Skip to content

Commit

Permalink
Extract Serializer building
Browse files Browse the repository at this point in the history
  • Loading branch information
oriolgual committed May 21, 2012
1 parent 0a93638 commit d8170a8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/hypermodel/resource.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'hypermodel/serializers/mongoid'
require 'hypermodel/serializer'

module Hypermodel
# Public: Responsible for building the response in JSON-HAL format. It is
Expand All @@ -18,7 +18,7 @@ class Resource
#
# Returns a flattened Array with the hierarchy of records.
TraverseUpwards = lambda do |record|
serializer = Serializers::Mongoid.new(record)
serializer = Serializer.build(record)

parent_name, parent_resource = (
serializer.embedding_resources.first || serializer.resources.first
Expand All @@ -43,7 +43,7 @@ class Resource
# choose the corresponding serializer.
def initialize(record, controller)
@record = record
@serializer = Serializers::Mongoid.new(record)
@serializer = Serializer.build(record)
@controller = controller
end

Expand Down
13 changes: 13 additions & 0 deletions lib/hypermodel/serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'hypermodel/serializers/mongoid'

module Hypermodel
# Private: Responsible for instantiating the correct serializer for a given
# record. Right now only works with Mongoid.
class Serializer

# Public: Returns a matching Serializer inspecting the ORM of the record.
def self.build(record)
Serializers::Mongoid.new(record)
end
end
end

0 comments on commit d8170a8

Please sign in to comment.