Module: Acfs::Resource::Locatable

Extended by:
ActiveSupport::Concern
Included in:
Acfs::Resource
Defined in:
lib/acfs/resource/locatable.rb

Overview

Provide methods for generation URLs for resources.

Examples:

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"

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#need_primary_key?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:



125
126
127
# File 'lib/acfs/resource/locatable.rb', line 125

def need_primary_key?
  true
end

#primary_key?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:



131
132
133
# File 'lib/acfs/resource/locatable.rb', line 131

def primary_key?
  respond_to?(:id) && !id.nil?
end

#url(**opts) ⇒ String

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

Examples:

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

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

Returns:

See Also:



115
116
117
118
119
120
121
# 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