class Yaks::Mapper::Link

A Yaks::Mapper::Link is part of a mapper's configuration. It captures what is set through the mapper's class level `#link` function, and is capable of generating a `Yaks::Resource::Link` for a given mapper instance (and hence subject).

@example

link :self, 'http://api.foo.org/users/{id}', title: ->{ "User #{object.name}" }
link :profile, 'http://apidocs.foo.org/profiles/users'
link 'http://apidocs.foo.org/rels/friends', 'http://api.foo.org/users/{id}/friends?page={page}', expand: [:id]

It takes a relationship identifier, a URI template and an options hash.

@param rel [Symbol|String] Either a registered relationship type (Symbol)

or a relationship URI. See [RFC5988 Web Linking](http://tools.ietf.org/html/rfc5988)

@param template [String] A [RFC6570](tools.ietf.org/html/rfc6570) URI template @param template [Symbol] A method name that generates the link. No more expansion is done afterwards @option expand [Boolean] pass false to pass on the URI template in the response,

instead of expanding the variables

@option expand [Array] pass a list of variable names to only expand those,

and return a partially expanded URI template in the response

@option title [String] Give the link a title @option title [#to_proc] Block that returns the title. If it takes an argument,

it will receive the mapper instance as argument. Otherwise it is evaluated in the mapper context

Public Class Methods

create(*args) click to toggle source
# File lib/yaks/mapper/link.rb, line 30
def self.create(*args)
  args, options = extract_options(args)
  new(rel: args.first, template: args.last, options: options)
end

Public Instance Methods

add_to_resource(resource, mapper, _context) click to toggle source
# File lib/yaks/mapper/link.rb, line 35
def add_to_resource(resource, mapper, _context)
  if options[:remove]
    return resource.with(links: resource.links.reject {|link| link.rel?(rel)})
  end

  resource_link = map_to_resource_link(mapper)
  return resource unless resource_link

  if options[:replace]
    resource.with(links: resource.links.reject {|link| link.rel?(rel)} << resource_link)
  else
    resource.add_link(resource_link)
  end
end
rel?(rel) click to toggle source
# File lib/yaks/mapper/link.rb, line 50
def rel?(rel)
  rel().eql? rel
end
templated?() click to toggle source

A link is templated if it does not expand, or only partially

# File lib/yaks/mapper/link.rb, line 55
def templated?
  !options.fetch(:expand) { true }.equal? true
end

Private Instance Methods