module Acfs::Resource::Locatable

Provide methods for generation URLs for resources.

@example

class User
  service AccountService # With base URL `http://acc.svr`
end
User.url    # => "http://acc.svr/users"
User.url(5) # => "http://acc.svr/users/5"

Public Instance Methods

need_primary_key?() click to toggle source

@api private Return true if resource needs a primary key (id) for singular actions.

# File lib/acfs/resource/locatable.rb, line 125
def need_primary_key?
  true
end
primary_key?() click to toggle source

@api private Return true if resource has a primary key (id) set.

# File lib/acfs/resource/locatable.rb, line 131
def primary_key?
  respond_to?(:id) && !id.nil?
end
url(**opts) click to toggle source

Return URL for this resource. Resource if will be appended as suffix if present.

@example

user.new.url # => "http://users.srv.org/users"

user = User.find 5
Acfs.run
user.url # => "http://users.srv.org/users/5"

@return [ String ] Generated URL. @see ClassMethods#url

# File lib/acfs/resource/locatable.rb, line 115
def url(**opts)
  return nil if need_primary_key? && !primary_key?

  self.class.service
      .location(self.class, **opts, action: :read)
      .build(attributes).str
end