Class: Intermodal::Proxies::LinkingResources
- Inherits:
-
Object
- Object
- Intermodal::Proxies::LinkingResources
- 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
Book #1 has 3 authors, Author #1, #2, and #3
Expected output:
{ "books": { "author_ids": [ 1, 2, 3 ] } }
<books>
<>
<>1</author_id>
<author_id>2</>
<>3</author_id>
</>
</books>
Instance Attribute Summary collapse
-
#collection ⇒ Object
Returns the value of attribute collection.
-
#linked_resource_name ⇒ Object
Returns the value of attribute linked_resource_name.
-
#parent_id ⇒ Object
Returns the value of attribute parent_id.
-
#parent_resource_name ⇒ Object
Returns the value of attribute parent_resource_name.
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
-
#initialize(parent_resource_name, options = {}) ⇒ LinkingResources
constructor
USAGE: Intermodal::Proxies::LinkingResources.new(:parent, :to => :linked_resources, :with => collection).
- #linked_resource_element_name ⇒ Object
- #presentation ⇒ Object
- #to_json(options = {}) ⇒ Object
- #to_xml(options = {}) ⇒ Object
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, = {}) @parent_resource_name = parent_resource_name @collection = [:with] @linked_resource_name = [:to] @parent_id = ([:parent_id] ? [:parent_id].to_i : nil ) # nil should be nil, not 0 end |
Instance Attribute Details
#collection ⇒ Object
Returns the value of attribute collection.
22 23 24 |
# File 'lib/intermodal/proxies/linking_resources.rb', line 22 def collection @collection end |
#linked_resource_name ⇒ Object
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_id ⇒ Object
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_name ⇒ Object
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( = {}) root = [:root] || parent_resource_name (root ? { root => presentation } : presentation) end |
#linked_resource_element_name ⇒ Object
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 |
#presentation ⇒ Object
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( = {}) as_json().to_json end |
#to_xml(options = {}) ⇒ Object
43 44 45 46 |
# File 'lib/intermodal/proxies/linking_resources.rb', line 43 def to_xml( = {}) root = [:root] || parent_resource_name presentation.to_xml(:root => root) end |