Module: Roar::Hypermedia

Defined in:
lib/roar/hypermedia.rb

Overview

Define hypermedia links in your representations.

Example:

class Order
  include Roar: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, DefinitionOptions Classes: Hyperlink

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

this is not called by rendering as we go via ::links_config.


42
43
44
45
46
# File 'lib/roar/hypermedia.rb', line 42

def links # this is _not_ called by rendering as we go via ::links_config.
  tuples = (@links||[]).collect { |link| [link.rel, link] }
  # tuples.to_h
  ::Hash[tuples] # TODO: tuples.to_h when dropping < 2.1.
end

Class Method Details

.included(base) ⇒ Object


36
37
38
# File 'lib/roar/hypermedia.rb', line 36

def self.included(base)
  base.extend ClassMethods
end