Permalink
Browse files

Extract Serializer building

  • Loading branch information...
1 parent 0a93638 commit d8170a8fd1df16c53195942c03cf46e6e4afbe27 @oriolgual oriolgual committed May 21, 2012
Showing with 16 additions and 3 deletions.
  1. +3 −3 lib/hypermodel/resource.rb
  2. +13 −0 lib/hypermodel/serializer.rb
@@ -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
@@ -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
@@ -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
@@ -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.