Module: Her::Model::Paths::ClassMethods

Defined in:
lib/her/model/paths.rb

Instance Method Summary collapse

Instance Method Details

#build_request_path(path = nil, parameters = {}) ⇒ Object

Return a custom path based on the collection path and variable parameters

Examples:

class User
  include Her::Model
  collection_path "/utilisateurs"
end

User.all # Fetched via GET /utilisateurs


62
63
64
65
66
67
68
69
70
71
72
# File 'lib/her/model/paths.rb', line 62

def build_request_path(path=nil, parameters={})
  unless path.is_a?(String)
    parameters = path || {}
    path = parameters.include?(:id) && !parameters[:id].nil? ? resource_path : collection_path
  end

  path.gsub(/:([\w_]+)/) do
    # Look for :key or :_key, otherwise raise an exception
    parameters.delete($1.to_sym) || parameters.delete("_#{$1}".to_sym) || raise(Her::Errors::PathError.new("Missing :_#{$1} parameter to build the request path (#{path})."))
  end
end

#collection_path(path = nil) ⇒ Object

Defines a custom collection path for the resource

Examples:

class User
  include Her::Model
  collection_path "/users"
end


27
28
29
30
31
32
33
34
35
# File 'lib/her/model/paths.rb', line 27

def collection_path(path=nil)
  @her_collection_path ||= begin
    superclass.collection_path.dup if superclass.respond_to?(:collection_path)
  end

  return @her_collection_path unless path
  @her_resource_path = "#{path}/:id"
  @her_collection_path = path
end

#resource_path(path = nil) ⇒ Object

Defines a custom resource path for the resource

Examples:

class User
  include Her::Model
  resource_path "/users/:id"
end


44
45
46
47
48
49
50
51
# File 'lib/her/model/paths.rb', line 44

def resource_path(path=nil)
  @her_resource_path ||= begin
    superclass.resource_path.dup if superclass.respond_to?(:resource_path)
  end

  return @her_resource_path unless path
  @her_resource_path = path
end

#root_element(value = nil) ⇒ Object

Return or change the value of ‘root_element`



75
76
77
78
# File 'lib/her/model/paths.rb', line 75

def root_element(value=nil)
  return @root_element if value.nil?
  @root_element = value
end