class ATD::Route

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 {ATD::App#call the rack app}.

Attributes

actions[RW]
app[R]
args[RW]
block[RW]
filename[RW]
headers[RW]
method[RW]
output[RW]
path[RW]
status_code[RW]

Public Class Methods

new(*args, &block) click to toggle source

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

# File lib/atd.rb, line 33
def initialize(*args, &block)
  @args, @block, @path, @output, @actions, @status_code, @filename = nil
  @status_code = 200
  @headers = {}
  @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

Public Instance Methods

app=(app_name) click to toggle source

This works differently from a standard setter because is makes sure that a {Route} can belong to only one {App}.

# File lib/atd.rb, line 44
def app=(app_name)
  old_app = Object.const_get(@app)
  new_app = Object.const_get(app_name.to_sym)
  old_app.routes -= self if old_app.routes.is_a?(Array) && old_app.routes.include?(self)
  new_app.routes.nil? ? new_app.routes = Array(self) : new_app.routes += Array(self)
  @app = app_name
end

Private Instance Methods

parse_args(path = "", output = "", *args, &block) click to toggle source

This should also manage @method at some point

# File lib/atd.rb, line 101
def parse_args(path = "", output = "", *args, &block)
  args = Hash(args[0]) if args.is_a? Array
  if output.is_a? Hash
    args.merge(output)
    output = ""
  end
  @args = Hash(@args).merge(args)
  # rubocop:disable Style/EmptyElse
  if !path.is_a? ATD::Route
    @block = block
    @path = path
    @output = output
  else
    # Maybe here it would make sense to assign path and output into args
  end
  # rubocop:enable Style/EmptyElse
  # @output should be whatever the input is unless the input is a controller/action or the input is_file_string?
  if @output =~ /^\w*#\w*$/ # Check if @path is a controller#action combo
    controller, action = @output.split("#")
    @action = Object.const_get(controller.to_sym).method(action.to_sym)
    @output = @action.call
  end
  # These next few lines are working on the assumption that if the file exists we want it, and the precompiler is
  # counting on that.
  output_full_path = asset @output
  @output = File.new(output_full_path) if File.exist?(output_full_path) && !Dir.exist?(output_full_path)
  @method += Array(args[:respond_to]) unless args[:respond_to].nil?
  @method -= Array(args[:ignore]) unless args[:ignore].nil?
  @status_code = args[:status] unless args[:status].nil?
  @status_code = args[:status_code] unless args[:status_code].nil?
  Compilation.precompile(self)
  @headers["content-type"] = MIME::Types.of(@filename)[0].to_s unless @filename.nil?
  @block = @actions unless @actions.nil?
  self
end