Class: Intermodal::Proxies::LinkingResources

Inherits:
Object
  • Object
show all
Defined in:
lib/intermodal/proxies/linking_resources.rb

Overview

This class is necessary to create the correct output for linked resources. Example:

books n-to-n authors
Book #1 has 3 authors, Author #1, #2, and #3

Expected output:

{ "books": { "author_ids": [ 1, 2, 3 ] } }

<books>
  <author_ids>
    <author_id>1</author_id>
    <author_id>2</author_id>
    <author_id>3</author_id>
  </author_ids>
</books>

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent_resource_name, options = {}) ⇒ LinkingResources

USAGE:

Intermodal::Proxies::LinkingResources.new(:parent, :to => :linked_resources, :with => collection)


27
28
29
30
31
32
# File 'lib/intermodal/proxies/linking_resources.rb', line 27

def initialize(parent_resource_name, options = {})
  @parent_resource_name = parent_resource_name
  @collection = options[:with]
  @linked_resource_name = options[:to]
  @parent_id = (options[:parent_id] ? options[:parent_id].to_i : nil ) # nil should be nil, not 0
end

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



22
23
24
# File 'lib/intermodal/proxies/linking_resources.rb', line 22

def collection
  @collection
end

#linked_resource_nameObject

Returns the value of attribute linked_resource_name.



22
23
24
# File 'lib/intermodal/proxies/linking_resources.rb', line 22

def linked_resource_name
  @linked_resource_name
end

#parent_idObject

Returns the value of attribute parent_id.



22
23
24
# File 'lib/intermodal/proxies/linking_resources.rb', line 22

def parent_id
  @parent_id
end

#parent_resource_nameObject

Returns the value of attribute parent_resource_name.



22
23
24
# File 'lib/intermodal/proxies/linking_resources.rb', line 22

def parent_resource_name
  @parent_resource_name
end

Instance Method Details

#as_json(options = {}) ⇒ Object



38
39
40
41
# File 'lib/intermodal/proxies/linking_resources.rb', line 38

def as_json(options = {})
  root = options[:root] || parent_resource_name
  (root ? { root => presentation } : presentation)
end

#linked_resource_element_nameObject



52
53
54
# File 'lib/intermodal/proxies/linking_resources.rb', line 52

def linked_resource_element_name
  "#{linked_resource_name.to_s.singularize}_ids"
end

#presentationObject



48
49
50
# File 'lib/intermodal/proxies/linking_resources.rb', line 48

def presentation
  { linked_resource_element_name => collection.to_a, :id => parent_id }
end

#to_json(options = {}) ⇒ Object



34
35
36
# File 'lib/intermodal/proxies/linking_resources.rb', line 34

def to_json(options = {})
  as_json(options).to_json
end

#to_xml(options = {}) ⇒ Object



43
44
45
46
# File 'lib/intermodal/proxies/linking_resources.rb', line 43

def to_xml(options = {})
  root = options[:root] || parent_resource_name
  presentation.to_xml(:root => root)
end