class Flame::Validators::RouteArgumentsValidator

Compare arguments from path and from controller's action

Public Class Methods

new(ctrl, path, action) click to toggle source
# File lib/flame/validators.rb, line 10
def initialize(ctrl, path, action)
        @ctrl = ctrl
        @path = Flame::Path.new(path)
        @action = action
end

Public Instance Methods

valid?() click to toggle source
# File lib/flame/validators.rb, line 16
def valid?
        extra_valid? && order_valid?
end

Private Instance Methods

action_arguments() click to toggle source

Take args from controller's action

# File lib/flame/validators.rb, line 51
def action_arguments
        ## Get all parameters (arguments) from method
        ## Than collect and sort parameters into hash
        @action_arguments ||= @ctrl.instance_method(@action).parameters
                .each_with_object(req: [], opt: []) do |param, hash|
                        ## Only required parameters must be in `:req`
                        hash[param[0]] << param[1]
                end
end
all_extra_arguments() click to toggle source

Calculate path and action extra arguments

# File lib/flame/validators.rb, line 62
def all_extra_arguments
        %i[req opt].each_with_object({}) do |type, extra_arguments|
                extra_arguments[type] = {
                        ctrl: action_arguments[type] - path_arguments[type],
                        path: path_arguments[type] - action_arguments[type]
                }
        end
end
extra_valid?() click to toggle source
# File lib/flame/validators.rb, line 22
def extra_valid?
        extra_arguments = first_extra_arguments
        ## Raise error if extra arguments
        return true unless extra_arguments
        raise Errors::RouteExtraArgumentsError.new(
                @ctrl, @action, @path, extra_arguments
        )
end
first_extra_arguments() click to toggle source
# File lib/flame/validators.rb, line 71
def first_extra_arguments
        ## Get hash of any extra arguments
        all_extra_arguments.find do |type, extra_arguments|
                found = extra_arguments.find do |place, args|
                        break { place: place, type: type, args: args } if args.any?
                end
                break found if found
        end
end
first_wrong_ordered_arguments() click to toggle source
# File lib/flame/validators.rb, line 81
def first_wrong_ordered_arguments
        opt_arguments = action_arguments[:opt].zip(path_arguments[:opt])
        opt_arguments.map! do |args|
                args.map { |arg| Flame::Path::PathPart.new(arg, arg: :opt) }
        end
        opt_arguments.find do |action_argument, path_argument|
                action_argument != path_argument
        end
end
order_valid?() click to toggle source
# File lib/flame/validators.rb, line 31
def order_valid?
        wrong_ordered_arguments = first_wrong_ordered_arguments
        return true unless wrong_ordered_arguments
        raise Errors::RouteArgumentsOrderError.new(
                @path, wrong_ordered_arguments
        )
end
path_arguments() click to toggle source

Split path to args array

# File lib/flame/validators.rb, line 40
def path_arguments
        @path_arguments ||= @path.parts
                .each_with_object(req: [], opt: []) do |part, hash|
                        ## Take only argument parts
                        next unless part.arg?
                        ## Memorize arguments
                        hash[part.opt_arg? ? :opt : :req] << part.clean.to_sym
                end
end