module Roar::Hypermedia
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)
Attributes
links[W]
public API: links
(only helpful in clients, though).
Public Class Methods
included(base)
click to toggle source
# File lib/roar/hypermedia.rb, line 36 def self.included(base) base.extend ClassMethods end
Public Instance Methods
links()
click to toggle source
# 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
Private Instance Methods
compile_links_for(configs, *args)
click to toggle source
# File lib/roar/hypermedia.rb, line 58 def compile_links_for(configs, *args) configs.collect do |config| options, block = config.first, config.last href = run_link_block(block, *args) or next prepare_link_for(href, options) end.compact # FIXME: make this less ugly. end
prepare_link_for(href, options)
click to toggle source
# File lib/roar/hypermedia.rb, line 67 def prepare_link_for(href, options) options = options.merge(href.is_a?(::Hash) ? href : {href: href}) Hyperlink.new(options) end
prepare_links!(options)
click to toggle source
Create hypermedia links for this instance by invoking their blocks. This is called in links: getter: {}.
# File lib/roar/hypermedia.rb, line 51 def prepare_links!(options) return [] if (options[:user_options] || {})[:links] == false link_configs = representable_attrs["links"].link_configs compile_links_for(link_configs, options) end
run_link_block(block, options)
click to toggle source
# File lib/roar/hypermedia.rb, line 72 def run_link_block(block, options) instance_exec(options[:user_options], &block) end