Class: Hypermodel::Serializers::Mongoid

Inherits:
Object
  • Object
show all
Defined in:
lib/hypermodel/serializers/mongoid.rb

Overview

Internal: A Mongoid serializer that complies with the Hypermodel Serializer API.

It is used by Hypermodel::Resource to extract the attributes and resources of a given record.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ Mongoid

Public: Initializes a Serializer::Mongoid.

record - A Mongoid instance of a model.



19
20
21
22
# File 'lib/hypermodel/serializers/mongoid.rb', line 19

def initialize(record)
  @record     = record
  @attributes = record.attributes.dup
end

Instance Attribute Details

#attributesObject (readonly)

Public: Returns the attributes of the Mongoid instance



14
15
16
# File 'lib/hypermodel/serializers/mongoid.rb', line 14

def attributes
  @attributes
end

#recordObject (readonly)

Public: Returns the Mongoid instance



11
12
13
# File 'lib/hypermodel/serializers/mongoid.rb', line 11

def record
  @record
end

Instance Method Details

#embedded_resourcesObject

Public: Returns a Hash with the embedded resources attributes. It will be used by Hypermodel::Resource.

An example of an embedded resource could be the reviews of a post, or the addresses of a company. But you can really embed whatever you like.

An example of the returning Hash could be the following:

{"comments"=>
  [
    {"_id"=>"4fb941cb82b4d46162000007", "body"=>"Comment 1"},
    {"_id"=>"4fb941cb82b4d46162000008", "body"=>"Comment 2"}
  ]
}


71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hypermodel/serializers/mongoid.rb', line 71

def embedded_resources
  return {} if embedded_relations.empty?

  embedded_relations.inject({}) do |acc, (name, )|
    if attributes = extract_embedded_attributes(name)
      @attributes.delete(name)
      acc.update(name => attributes)
    end
    acc
  end
end

#embedding_resourcesObject

Public: Returns a Hash with the resources that are embedding us (our immediate ancestors).



85
86
87
88
89
90
91
92
93
# File 'lib/hypermodel/serializers/mongoid.rb', line 85

def embedding_resources
  return {} if embedded_relations.empty?

  embedded = select_embedded_by_type(::Mongoid::Relations::Embedded::In)

  embedded.inject({}) do |acc, (name, _)|
    acc.update(name => @record.send(name))
  end
end

#resourcesObject

Public: Returns a Hash with the resources that are linked to the record. It will be used by Hypermodel::Resource.

An example of a linked resource could be the author of a post. Think of ‘/authors/:author_id`

The format of the returned Hash must be the following:

{resource_name: resource_instance}

‘resource_name` can be either a Symbol or a String.



35
36
37
38
39
40
41
# File 'lib/hypermodel/serializers/mongoid.rb', line 35

def resources
  relations = select_relations_by_type(::Mongoid::Relations::Referenced::In)

  relations.inject({}) do |acc, (name, _)|
    acc.update(name => @record.send(name))
  end
end

#sub_resourcesObject

Public: Returns a Hash with the sub resources that are linked to the record. It will be used by Hypermodel::Resource. These resources need to be differentiated so Hypermodel::Resource can build the url.

An example of a linked sub resource could be comments of a post. Think of ‘/posts/:id/comments`

The format of the returned Hash must be the following:

{:sub_resource, :another_subresource}


53
54
55
# File 'lib/hypermodel/serializers/mongoid.rb', line 53

def sub_resources
  select_relations_by_type(::Mongoid::Relations::Referenced::Many).keys
end