Class: Pakyow::Endpoints

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/pakyow/endpoints.rb

Overview

Lookup for endpoints.

Instance Method Summary collapse

Constructor Details

#initializeEndpoints

Returns a new instance of Endpoints.



14
15
16
# File 'lib/pakyow/endpoints.rb', line 14

def initialize
  @endpoints = []
end

Instance Method Details

#<<(endpoint) ⇒ Object

Adds an endpoint.



26
27
28
# File 'lib/pakyow/endpoints.rb', line 26

def <<(endpoint)
  @endpoints << endpoint
end

#find(name:) ⇒ Object



18
19
20
21
22
# File 'lib/pakyow/endpoints.rb', line 18

def find(name:)
  @endpoints.find { |endpoint|
    endpoint.name == name
  }
end

#load(object_with_endpoints) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/pakyow/endpoints.rb', line 30

def load(object_with_endpoints)
  if object_with_endpoints.respond_to?(:endpoints)
    object_with_endpoints.endpoints.each do |endpoint|
      self << endpoint
    end
  end
end

#method(name) ⇒ Object



52
53
54
# File 'lib/pakyow/endpoints.rb', line 52

def method(name)
  endpoint_with_name(name)&.method
end

#path(name, hashlike_object = nil, **params) ⇒ Object

Builds the path to a named route.

Examples:

Build the path to the new route within the post group:

path(:post_new)
# => "/posts/new"

Build the path providing a value for post_id:

path(:post_edit, post_id: 1)
# => "/posts/1/edit"


48
49
50
# File 'lib/pakyow/endpoints.rb', line 48

def path(name, hashlike_object = nil, **params)
  endpoint_with_name(name)&.path(hashlike_object, **params)
end

#path_to(*names, **params) ⇒ Object

Builds the path to a route, following a trail of names.

Examples:

Build the path to the new route within the post group:

path_to(:post, :new)
# => "/posts/new"

Build the path providing a value for post_id:

path_to(:post, :edit, post_id: 1)
# => "/posts/1/edit"


66
67
68
# File 'lib/pakyow/endpoints.rb', line 66

def path_to(*names, **params)
  path(names.join("_").to_sym, **params)
end