Skip to content

Commit

Permalink
Try a better naming
Browse files Browse the repository at this point in the history
  • Loading branch information
oriolgual committed May 18, 2012
1 parent 15b183a commit adc633a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
10 changes: 6 additions & 4 deletions lib/hypermodel/resource.rb
Expand Up @@ -3,8 +3,10 @@
# Next step is to select which fields to include in the output. # Next step is to select which fields to include in the output.
module Hypermodel module Hypermodel
class Resource class Resource
def initialize(resource, controller) # TODO: Detect resource type (AR, DM, Mongoid, etc..) and create the
@serializer = Serializers::Mongoid.new(resource) # corresponding serializer.
def initialize(record, controller)
@serializer = Serializers::Mongoid.new(record)
@controller = controller @controller = controller
end end


Expand All @@ -13,13 +15,13 @@ def to_json(*opts)
end end


def links def links
hash = { self: { href: @controller.polymorphic_url(@serializer.resource) } } hash = { self: { href: @controller.polymorphic_url(@serializer.record) } }
@serializer.resources.each do |name, resource| @serializer.resources.each do |name, resource|
hash.update(name => {href: @controller.polymorphic_url(resource)}) hash.update(name => {href: @controller.polymorphic_url(resource)})
end end


@serializer.sub_resources.each do |sub_resource| @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 end


{ _links: hash } { _links: hash }
Expand Down
4 changes: 2 additions & 2 deletions lib/hypermodel/responder.rb
Expand Up @@ -13,10 +13,10 @@ def self.call(*args)
controller.render json: responder controller.render json: responder
end end


def initialize(resource_name, action, resource, controller) def initialize(resource_name, action, record, controller)
@resource_name = resource_name @resource_name = resource_name
@action = action @action = action
@resource = Resource.new(resource, controller) @resource = Resource.new(record, controller)
end end


def to_json(*opts) def to_json(*opts)
Expand Down
16 changes: 8 additions & 8 deletions lib/hypermodel/serializers/mongoid.rb
@@ -1,10 +1,10 @@
module Hypermodel module Hypermodel
module Serializers module Serializers
class Mongoid class Mongoid
attr_reader :resource, :attributes attr_reader :record, :attributes
def initialize(resource) def initialize(record)
@resource = resource @record = record
@attributes = resource.attributes.dup @attributes = record.attributes.dup
end end


def resources def resources
Expand Down Expand Up @@ -42,22 +42,22 @@ def extract_embedded_attributes(name, metadata)
relation = metadata.relation relation = metadata.relation


if relation == ::Mongoid::Relations::Embedded::Many 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 elsif relation == ::Mongoid::Relations::Embedded::One
(embedded = resource.send(name)) ? embedded.attributes : nil (embedded = @record.send(name)) ? embedded.attributes : nil
else else
raise "Embedded relation type not implemented: #{relation}" raise "Embedded relation type not implemented: #{relation}"
end end
end end


def embedded_relations def embedded_relations
@embedded_relations ||= @resource.relations.select do |_, metadata| @embedded_relations ||= @record.relations.select do |_, metadata|
metadata.relation.name =~ /Embedded/ metadata.relation.name =~ /Embedded/
end end
end end


def referenced_relations def referenced_relations
@referenced_relations ||= @resource.relations.select do |_, metadata| @referenced_relations ||= @record.relations.select do |_, metadata|
metadata.relation.name =~ /Referenced/ metadata.relation.name =~ /Referenced/
end end
end end
Expand Down

0 comments on commit adc633a

Please sign in to comment.