From adc633a301a53762346eb9f73899fe74e778e6c3 Mon Sep 17 00:00:00 2001 From: Oriol Gual Date: Fri, 18 May 2012 21:02:31 +0200 Subject: [PATCH] Try a better naming --- lib/hypermodel/resource.rb | 10 ++++++---- lib/hypermodel/responder.rb | 4 ++-- lib/hypermodel/serializers/mongoid.rb | 16 ++++++++-------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/hypermodel/resource.rb b/lib/hypermodel/resource.rb index d859d8b..96e80a9 100644 --- a/lib/hypermodel/resource.rb +++ b/lib/hypermodel/resource.rb @@ -3,8 +3,10 @@ # Next step is to select which fields to include in the output. module Hypermodel class Resource - def initialize(resource, controller) - @serializer = Serializers::Mongoid.new(resource) + # TODO: Detect resource type (AR, DM, Mongoid, etc..) and create the + # corresponding serializer. + def initialize(record, controller) + @serializer = Serializers::Mongoid.new(record) @controller = controller end @@ -13,13 +15,13 @@ def to_json(*opts) end def links - hash = { self: { href: @controller.polymorphic_url(@serializer.resource) } } + hash = { self: { href: @controller.polymorphic_url(@serializer.record) } } @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])}) + hash.update(sub_resource => {href: @controller.polymorphic_url([@serializer.record, sub_resource])}) end { _links: hash } diff --git a/lib/hypermodel/responder.rb b/lib/hypermodel/responder.rb index 321e36a..1efa5ad 100644 --- a/lib/hypermodel/responder.rb +++ b/lib/hypermodel/responder.rb @@ -13,10 +13,10 @@ def self.call(*args) controller.render json: responder end - def initialize(resource_name, action, resource, controller) + def initialize(resource_name, action, record, controller) @resource_name = resource_name @action = action - @resource = Resource.new(resource, controller) + @resource = Resource.new(record, controller) end def to_json(*opts) diff --git a/lib/hypermodel/serializers/mongoid.rb b/lib/hypermodel/serializers/mongoid.rb index 59f8eab..ec098fe 100644 --- a/lib/hypermodel/serializers/mongoid.rb +++ b/lib/hypermodel/serializers/mongoid.rb @@ -1,10 +1,10 @@ module Hypermodel module Serializers class Mongoid - attr_reader :resource, :attributes - def initialize(resource) - @resource = resource - @attributes = resource.attributes.dup + attr_reader :record, :attributes + def initialize(record) + @record = record + @attributes = record.attributes.dup end def resources @@ -42,22 +42,22 @@ def extract_embedded_attributes(name, metadata) relation = metadata.relation if relation == ::Mongoid::Relations::Embedded::Many - @resource.send(name).map { |embedded| embedded.attributes } + @record.send(name).map { |embedded| embedded.attributes } elsif relation == ::Mongoid::Relations::Embedded::One - (embedded = resource.send(name)) ? embedded.attributes : nil + (embedded = @record.send(name)) ? embedded.attributes : nil else raise "Embedded relation type not implemented: #{relation}" end end def embedded_relations - @embedded_relations ||= @resource.relations.select do |_, metadata| + @embedded_relations ||= @record.relations.select do |_, metadata| metadata.relation.name =~ /Embedded/ end end def referenced_relations - @referenced_relations ||= @resource.relations.select do |_, metadata| + @referenced_relations ||= @record.relations.select do |_, metadata| metadata.relation.name =~ /Referenced/ end end