Permalink
Browse files

Try a better naming

  • Loading branch information...
1 parent 15b183a commit adc633a301a53762346eb9f73899fe74e778e6c3 @oriolgual oriolgual committed May 18, 2012
Showing with 16 additions and 14 deletions.
  1. +6 −4 lib/hypermodel/resource.rb
  2. +2 −2 lib/hypermodel/responder.rb
  3. +8 −8 lib/hypermodel/serializers/mongoid.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 }
@@ -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)
@@ -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

0 comments on commit adc633a

Please sign in to comment.