Class: ATD::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/atd.rb

Overview

So called because each instance stores a route, and will be called if that route is reached. A route for the purposes of ATD is a parser that will be fed env in the rack app.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Route

The first two arguments must me the path and the output.



26
27
28
29
30
31
# File 'lib/atd.rb', line 26

def initialize(*args, &block)
  @method = [:get, :post, :put, :patch, :delete]
  @method = [] if args.last.is_a?(Hash) && !(args.last[:respond_to].nil? || args.last[:ignore].nil?)
  @app = :DefaultApp
  parse_args(*args, &block)
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



23
24
25
# File 'lib/atd.rb', line 23

def actions
  @actions
end

#appObject

Returns the value of attribute app.



23
24
25
# File 'lib/atd.rb', line 23

def app
  @app
end

#argsObject

Returns the value of attribute args.



23
24
25
# File 'lib/atd.rb', line 23

def args
  @args
end

#blockObject

Returns the value of attribute block.



23
24
25
# File 'lib/atd.rb', line 23

def block
  @block
end

#methodObject

Returns the value of attribute method.



23
24
25
# File 'lib/atd.rb', line 23

def method
  @method
end

#outputObject

Returns the value of attribute output.



23
24
25
# File 'lib/atd.rb', line 23

def output
  @output
end

#pathObject

Returns the value of attribute path.



23
24
25
# File 'lib/atd.rb', line 23

def path
  @path
end

Instance Method Details

#delete(path = nil, *args) ⇒ Object

Sets route to receive a delete request to path and execute the block provided (if one is provided)



67
68
69
70
71
72
73
74
# File 'lib/atd.rb', line 67

[:get, :post, :put, :delete, :patch].each do |method|
  define_method(method) do |*args, &block|
    @method = [method] if @method.length == 5
    @method += [method]
    @method.uniq!
    parse_args(*args, &block)
  end
end

#get(path = nil, *args) ⇒ Object

Sets route to receive a get request to path and execute the block provided (if one is provided)



# File 'lib/atd.rb', line 42


#patch(path = nil, *args) ⇒ Object

Sets route to receive a patch request to path and execute the block provided (if one is provided)



# File 'lib/atd.rb', line 57


#post(path = nil, *args) ⇒ Object

Sets route to receive a post request to path and execute the block provided (if one is provided)



# File 'lib/atd.rb', line 47


#put(path = nil, *args) ⇒ Object

Sets route to receive a put request to path and execute the block provided (if one is provided)



# File 'lib/atd.rb', line 52


#to_hObject

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.

Converts an instance of ATD::Route into it’s Hash representation. The format for the Hash is listed here



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/atd.rb', line 79

def to_h
  routes = {}
  routes[@path] = {}
  routes[@path][@method] = {}
  routes[@path][@method] = {
    output: @output,
    block: @block,
    args: @args,
    route: self
  }
  routes
end