Skip to content
Browse files

Refactor Mongoid serializer and extract Resource

  • Loading branch information...
1 parent 00b1280 commit 03cab1dbb6f369c6e6219b4c8ea11240a6eae022 @oriolgual oriolgual committed May 18, 2012
Showing with 47 additions and 28 deletions.
  1. +29 −0 lib/hypermodel/resource.rb
  2. +3 −3 lib/hypermodel/responder.rb
  3. +15 −25 lib/hypermodel/serializers/mongoid.rb
View
29 lib/hypermodel/resource.rb
@@ -0,0 +1,29 @@
+# This class is ORM agnostic. Wohoo!
+# Next step is to select which fields to include in the output.
+class Resource
+ def initialize(serializer, controller)
+ @serializer = serializer
+ @controller = controller
+ end
+
+ def to_json(*opts)
+ @serializer.attributes.update(links).update(embedded).to_json(*opts)
+ end
+
+ def links
+ hash = { self: { href: @controller.polymorphic_url(@serializer.resource) } }
+ @serializer.resources.each do |name, resource|
+ hash.update(name => {href: @controller.polymorphic_url(resource)})
+ end
+
+ @serializer.sub_resources.each do |sub_resource|
+ hash.update(sub_resource => {href: @controller.polymorphic_url([@serializer.resource, sub_resource])})
+ end
+
+ { _links: hash }
+ end
+
+ def embedded
+ @serializer.embedded_resources
+ end
+end
View
6 lib/hypermodel/responder.rb
@@ -1,8 +1,7 @@
require 'hypermodel/serializers/mongoid'
+require 'hypermodel/resource'
module Hypermodel
- Serializer = Serializers::Mongoid
-
class Responder
def self.call(*args)
controller = args[0]
@@ -18,7 +17,8 @@ def self.call(*args)
def initialize(resource_name, action, resource, controller)
@resource_name = resource_name
@action = action
- @resource = Serializer.new(resource, controller)
+ @serializer = Serializers::Mongoid.new(resource)
+ @resource = Resource.new(@serializer, controller)
end
def to_json(*opts)
View
40 lib/hypermodel/serializers/mongoid.rb
@@ -1,40 +1,31 @@
module Hypermodel
module Serializers
class Mongoid
- def initialize(resource, controller)
+ attr_reader :resource, :attributes
+ def initialize(resource)
@resource = resource
@attributes = resource.attributes.dup
- @controller = controller
end
- def to_json(*opts)
- attrs = @attributes.update(links).update(embedded)
- attrs.to_json(*opts)
- end
-
- def links
- hash = { self: { href: @controller.polymorphic_url(@resource) } }
-
- referenced_relations.each do |name, metadata|
- related = @resource.send(name)
- relation = metadata.relation
+ def resources
+ links = referenced_relations.select do |name, metadata|
+ metadata.relation == ::Mongoid::Relations::Referenced::In
+ end
- if relation == ::Mongoid::Relations::Referenced::In
- unless related.nil? || (related.respond_to?(:empty?) && related.empty?)
- hash.update(name => { href: @controller.polymorphic_url(related) })
- end
- elsif relation == ::Mongoid::Relations::Referenced::Many
- hash.update(name => { href: @controller.polymorphic_url([@resource, name]) })
- else
- raise "Referenced relation type not implemented: #{relation}"
- end
+ links.inject({}) do |acc, (name, metadata)|
+ acc.update(name => @resource.send(name))
+ end
+ end
+ def sub_resources
+ links = referenced_relations.select do |name, metadata|
+ metadata.relation == ::Mongoid::Relations::Referenced::Many
end
- { _links: hash }
+ links.keys
end
- def embedded
+ def embedded_resources
return {} if embedded_relations.empty?
embedded_relations.inject({ _embedded: {} }) do |acc, (name, metadata)|
@@ -47,7 +38,6 @@ def embedded
end
private
-
def extract_embedded_attributes(name, metadata)
relation = metadata.relation

0 comments on commit 03cab1d

Please sign in to comment.
Something went wrong with that request. Please try again.