Class: Ronin::Core::Params::Param

Inherits:
Object
  • Object
show all
Defined in:
lib/ronin/core/params/param.rb

Overview

Represents an individual param.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, required: false, default: nil, desc:) ⇒ 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.

Initializes the param.

Parameters:

  • name (Symbol)

    The name of the param.

  • type (Type)

    The type of the param.

  • required (Boolean) (defaults to: false)

    Whether the param is required or not.

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

    The optional default value or default value proc for the param.

  • desc (String)

    A short one-line description of the param.



74
75
76
77
78
79
80
# File 'lib/ronin/core/params/param.rb', line 74

def initialize(name,type, required: false, default: nil, desc: )
  @name     = name
  @type     = type
  @required = required
  @default  = default
  @desc     = desc
end

Instance Attribute Details

#defaultObject, ... (readonly)

The default value or proc for the param.

Returns:

  • (Object, Proc, nil)


47
48
49
# File 'lib/ronin/core/params/param.rb', line 47

def default
  @default
end

#descString (readonly)

The description of the param.

Returns:

  • (String)


52
53
54
# File 'lib/ronin/core/params/param.rb', line 52

def desc
  @desc
end

#nameSymbol (readonly)

The name of the param.

Returns:

  • (Symbol)


32
33
34
# File 'lib/ronin/core/params/param.rb', line 32

def name
  @name
end

#requiredBoolean (readonly)

Specifies whether the param is required or not.

Returns:

  • (Boolean)


42
43
44
# File 'lib/ronin/core/params/param.rb', line 42

def required
  @required
end

#typeTypes::Type (readonly)

The param type.

Returns:



37
38
39
# File 'lib/ronin/core/params/param.rb', line 37

def type
  @type
end

Instance Method Details

#coerce(value) ⇒ Object

Coerces the value for the param.

Parameters:

  • value (Object)

    The value to coerce.

Returns:

  • (Object)

    The coerced value.

Raises:



123
124
125
126
127
128
129
130
131
132
# File 'lib/ronin/core/params/param.rb', line 123

def coerce(value)
  case value
  when nil
    if required?
      raise(ValidationError,"param requires a non-nil value")
    end
  else
    @type.coerce(value)
  end
end

#default_valueObject

Returns the default value for the param.

Returns:

  • (Object)


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

def default_value
  if @default.respond_to?(:call) then @default.call
  else                                @default.dup
  end
end

#has_default?Boolean

Determines if the param has a default value.

Returns:

  • (Boolean)


96
97
98
# File 'lib/ronin/core/params/param.rb', line 96

def has_default?
  !@default.nil?
end

#required?Boolean

Determines if the param is required.

Returns:

  • (Boolean)


87
88
89
# File 'lib/ronin/core/params/param.rb', line 87

def required?
  @required
end