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

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

Instance Method Summary collapse

Instance Method Details

#collection_path(path = nil) ⇒ Object

Defines a custom collection path for the resource

Examples:

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


48
49
50
51
52
53
54
55
# File 'lib/her/model/paths.rb', line 48

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) ⇒ Object

Define the primary key field that will be used to find and save records

Examples:

class User
  include Her::Model
  primary_key 'UserId'
end

Parameters:

  • value (Symbol) (defaults to: nil)


32
33
34
35
36
37
38
39
# File 'lib/her/model/paths.rb', line 32

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

#resource_path(path = nil) ⇒ Object

Defines a custom resource path for the resource

Note that, if used in combination with resource_path, you may specify either the real primary key or the string ':id'. For example:

Examples:

class User
  include Her::Model
  resource_path "/users/:id"
end
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


80
81
82
83
84
85
86
# File 'lib/her/model/paths.rb', line 80

def resource_path(path = nil)
  if path.nil?
    @_her_resource_path ||= "#{root_element.to_s.pluralize}/:id"
  else
    @_her_resource_path = path
  end
end