Module: Ronin::Core::Params::Mixin::ClassMethods

Defined in:
lib/ronin/core/params/mixin.rb

Overview

Class methods.

Instance Method Summary collapse

Instance Method Details

#param(name, type = Types::String.new, required: false, default: nil, desc:, **kwargs) ⇒ Object

Defines a new param.

Examples:

Define a basic String param:

param :foo, desc: 'Foo param'

Define a param with an explicit type:

param :foo, String, desc: 'Foo param'

Define a required param:

param :foo, required: true, desc: 'A required param'

Define a String param with a validation regex:

param :foo, String, format: /\A...\z/, desc: 'Foo param'

Define a boolean param:

param :mode, Boolean

Define an enum param:

param :mode, Enum[:one, :two, :three]

Define a Regexp param:

param :pattern, Regexp

Define a URI param:

param :pattern, URI

Define a param with a specific type:

param :foo, Integer, desc: 'Foo param'

Define a param with type validations:

param :foo, Integer, min:  1,
                     max:  100,
                     desc: 'Foo param'

Define a param with a default value:

param :foo, Integer, default: 42,
                     desc:    'Foo param'

Define a param with a proc default value:

param :foo, Integer, default: ->{ rand(42) },
                     desc:    'Foo param'

Parameters:

  • name (Symbol)

    The name for the new param.

  • type (Class, Types::Type) (defaults to: Types::String.new)

    The type for the new param. Available types are:

  • required (Boolean) (defaults to: false)

    Specifies whether the param requires a value or is optional.

  • default (Object, nil) (defaults to: nil)

    The optional default value for the param.

  • desc (String)

    The description for the param. All defined params must have a description.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for the type class.



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/ronin/core/params/mixin.rb', line 184

def param(name,type=Types::String.new, required: false,
                                       default:  nil,
                                       desc: ,
                                       **kwargs)
  type = case type
         when Types::Type then type
         else                  Types.lookup(type).new(**kwargs)
         end

  params[name] = Param.new(name,type, required: required,
                                      default:  default,
                                      desc:     desc)
end

#paramsHash{Symbol => Param}

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.

All defined params.

Returns:

  • (Hash{Symbol => Param})


105
106
107
108
109
110
111
# File 'lib/ronin/core/params/mixin.rb', line 105

def params
  @params ||= if superclass < Mixin
                superclass.params.dup
              else
                {}
              end
end