Module: Roar::Representer::Feature::Hypermedia
- Includes:
- LinkConfigsMethod
- Defined in:
- lib/roar/representer/feature/hypermedia.rb
Overview
Define hypermedia links in your representations.
Example:
class Order
include Roar::Representer::JSON
property :id
link :self do
"http://orders/#{id}"
end
If you want more attributes, just pass a hash to #link.
link :rel => :next, :title => "Next, please!" do
"http://orders/#{id}"
end
If you need dynamic attributes, the block can return a hash.
link :preview do
{:href => image.url, :title => image.name}
end
Sometimes you need values from outside when the representation links are rendered. Just pass them to the render method, they will be available as block parameters.
link :self do |opts|
"http://orders/#{opts[:id]}"
end
model.to_json(:id => 1)
Defined Under Namespace
Modules: ClassMethods, LinkConfigsMethod Classes: Hyperlink, LinkCollection, LinksDefinition
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
Methods included from LinkConfigsMethod
Instance Attribute Details
#links ⇒ Object
50 51 52 |
# File 'lib/roar/representer/feature/hypermedia.rb', line 50 def links @links ||= LinkCollection.new end |
Class Method Details
.included(base) ⇒ Object
38 39 40 41 |
# File 'lib/roar/representer/feature/hypermedia.rb', line 38 def self.included(base) base.extend ClassMethods base.extend InheritableArray end |
Instance Method Details
#before_serialize(options = {}) ⇒ Object
43 44 45 46 |
# File 'lib/roar/representer/feature/hypermedia.rb', line 43 def before_serialize(={}) super() # Representer::Base prepare_links!() unless [:links] == false # DISCUSS: doesn't work when links are already setup (e.g. from #deserialize). end |
#links_array ⇒ Object
54 55 56 |
# File 'lib/roar/representer/feature/hypermedia.rb', line 54 def links_array links.values # FIXME: move to LinkCollection#to_a. end |
#links_array=(ary) ⇒ Object
58 59 60 61 62 |
# File 'lib/roar/representer/feature/hypermedia.rb', line 58 def links_array=(ary) # FIXME: move to LinkCollection self.links= LinkCollection.new ary.each { |lnk| links.add(lnk) } end |