# File lib/her/model/paths.rb, line 31 def primary_key(value = nil) @_her_primary_key ||= begin superclass.primary_key if superclass.respond_to?(:primary_key) end return @_her_primary_key unless value @_her_primary_key = value.to_sym end
module Her::Model::Paths::ClassMethods
Public Instance Methods
build_request_path(path = nil, parameters = {})
click to toggle source
Return a custom path based on the collection path and variable parameters
@private
# File lib/her/model/paths.rb, line 90 def build_request_path(path = nil, parameters = {}) parameters = parameters.try(:with_indifferent_access) unless path.is_a?(String) parameters = path.try(:with_indifferent_access) || parameters path = if parameters.include?(primary_key) && parameters[primary_key] && !parameters[primary_key].is_a?(Array) resource_path.dup else collection_path.dup end # Replace :id with our actual primary key path.gsub!(/(\A|\/):id(\Z|\/)/, "\\1:#{primary_key}\\2") end path.gsub(/:([\w_]+)/) do # Look for :key or :_key, otherwise raise an exception key = $1.to_sym value = parameters.delete(key) || parameters.delete(:"_#{key}") if value Faraday::Utils.escape value else raise(Her::Errors::PathError.new("Missing :_#{$1} parameter to build the request path. Path is `#{path}`. Parameters are `#{parameters.symbolize_keys.inspect}`.", $1)) end end end
build_request_path_from_string_or_symbol(path, params = {})
click to toggle source
@private
# File lib/her/model/paths.rb, line 119 def build_request_path_from_string_or_symbol(path, params = {}) path.is_a?(Symbol) ? "#{build_request_path(params)}/#{path}" : path end
collection_path(path = nil)
click to toggle source
Defines a custom collection path for the resource
@example
class User include Her::Model collection_path "/users" end
# File lib/her/model/paths.rb, line 47 def collection_path(path = nil) if path.nil? @_her_collection_path ||= root_element.to_s.pluralize else @_her_collection_path = path @_her_resource_path = "#{path}/:id" end end
primary_key(value = nil)
click to toggle source
Define the primary key field that will be used to find and save records
@example
class User include Her::Model primary_key 'UserId' end
@param [Symbol] value
resource_path(path = nil)
click to toggle source
Defines a custom resource path for the resource
@example
class User include Her::Model resource_path "/users/:id" end
Note that, if used in combination with resource_path
, you may specify either the real primary key or the string ':id'. For example:
@example
class User include Her::Model primary_key 'user_id' # This works because we'll have a user_id attribute resource_path '/users/:user_id' # This works because we replace :id with :user_id resource_path '/users/:id' end
# File lib/her/model/paths.rb, line 79 def resource_path(path = nil) if path.nil? @_her_resource_path ||= "#{root_element.to_s.pluralize}/:id" else @_her_resource_path = path end end