Class: Hypermodel::Resource
- Inherits:
-
Object
- Object
- Hypermodel::Resource
- Extended by:
- Forwardable
- Defined in:
- lib/hypermodel/resource.rb
Overview
Public: Responsible for building the response in JSON-HAL format. It is meant to be used by Hypermodel::Responder.
In future versions one will be able to subclass it and personalize a Resource for each diffent model, i.e. creating a PostResource.
Constant Summary collapse
- TraverseUpwards =
Public: Recursive functino that traverses a record’s referential hierarchy upwards.
Returns a flattened Array with the hierarchy of records.
lambda do |record| serializer = Serializers::Mongoid.new(record) parent_name, parent_resource = ( serializer..first || serializer.resources.first ) # If we have a parent if parent_resource # Recurse over parent hierarchies [TraverseUpwards[parent_resource], record].flatten else # Final case, we are the topmost parent: return ourselves [record] end end
Instance Method Summary collapse
-
#embedded ⇒ Object
Internal: Constructs the _embedded section of the response.
-
#initialize(record, controller) ⇒ Resource
constructor
Public: Initializes a Resource.
-
#links ⇒ Object
Internal: Constructs the _links section of the response.
-
#polymorphic_url(record_or_hash_or_array, options = {}) ⇒ Object
Internal: Returns the url wrapped in a Hash in HAL format.
-
#to_json(*opts) ⇒ Object
Public: Returns a Hash of the resource in JSON-HAL.
Constructor Details
#initialize(record, controller) ⇒ Resource
Public: Initializes a Resource.
record - A Mongoid instance of a model. controller - An ActionController instance.
TODO: Detect record type (ActiveRecord, DataMapper, Mongoid, etc..) and choose the corresponding serializer.
44 45 46 47 48 |
# File 'lib/hypermodel/resource.rb', line 44 def initialize(record, controller) @record = record @serializer = Serializers::Mongoid.new(record) @controller = controller end |
Instance Method Details
#embedded ⇒ Object
Internal: Constructs the _embedded section of the response.
Returns a Hash of the embedded resources of the resource.
78 79 80 |
# File 'lib/hypermodel/resource.rb', line 78 def { _embedded: } end |
#links ⇒ Object
Internal: Constructs the _links section of the response.
Returns a Hash of the links of the resource. It will include, at least, a link to itself.
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/hypermodel/resource.rb', line 61 def links _links = { self: polymorphic_url(record_with_ancestor_chain(@record)) } resources.each do |name, resource| _links.update(name => polymorphic_url(record_with_ancestor_chain(resource))) end sub_resources.each do |sub_resource| _links.update(sub_resource => polymorphic_url(record_with_ancestor_chain(@record) << sub_resource)) end { _links: _links } end |
#polymorphic_url(record_or_hash_or_array, options = {}) ⇒ Object
Internal: Returns the url wrapped in a Hash in HAL format.
83 84 85 |
# File 'lib/hypermodel/resource.rb', line 83 def polymorphic_url(record_or_hash_or_array, = {}) { href: @controller.polymorphic_url(record_or_hash_or_array, = {}) } end |
#to_json(*opts) ⇒ Object
Public: Returns a Hash of the resource in JSON-HAL.
opts - Options to pass to the resource to_json.
53 54 55 |
# File 'lib/hypermodel/resource.rb', line 53 def to_json(*opts) attributes.update(links).update().to_json(*opts) end |