Module: Ronin::Core::Params::Mixin
- Defined in:
- lib/ronin/core/params/mixin.rb
Overview
Allows classes to define configurable parameters.
Examples
class BaseClass
include Ronin::Core::Params::Mixin
end
class MyModule < BaseClass
param :str, desc: 'A basic string param'
param :feature_flag, Boolean, desc: 'A boolean param'
param :enum, Enum[:one, :two, :three],
desc: 'An enum param'
param :num1, Integer, desc: 'An integer param'
param :num2, Integer, default: 42,
desc: 'A param with a default value'
param :num3, Integer, default: ->{ rand(42) },
desc: 'A param with a dynamic default value'
param :float, Float, 'Floating point param'
param :url, URI, desc: 'URL param'
param :pattern, Regexp, desc: 'Regular Expression param'
end
mod = MyModule.new(params: {num1: 1, enum: :two})
mod.params
# => {:num2=>42, :num3=>25, :num1=>1, :enum=>:two}
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- Boolean =
The boolean param type.
Params::Types::Boolean
- Enum =
The enum param type.
Params::Types::Enum
Instance Attribute Summary collapse
-
#params ⇒ Hash{Symbol => Object}
The param values.
Class Method Summary collapse
-
.included(base_class) ⇒ Object
private
Adds ClassMethods to the given class.
Instance Method Summary collapse
-
#initialize(*arguments, params: nil, **kwargs, &block) ⇒ Object
Initializes the params.
-
#initialize_params ⇒ Object
Initializes #params.
-
#set_param(name, value) ⇒ Object
Sets a param's value.
-
#validate_params ⇒ true
Validates that all required params are set.
Instance Attribute Details
#params ⇒ Hash{Symbol => Object}
The param values.
204 205 206 |
# File 'lib/ronin/core/params/mixin.rb', line 204 def params @params end |
Class Method Details
.included(base_class) ⇒ Object
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.
Adds ClassMethods to the given class.
86 87 88 89 90 91 92 |
# File 'lib/ronin/core/params/mixin.rb', line 86 def self.included(base_class) unless base_class.kind_of?(Class) raise(TypeError,"#{Mixin} can only be included into classes") end base_class.extend ClassMethods end |
Instance Method Details
#initialize(*arguments, params: nil, **kwargs, &block) ⇒ Object
Initializes the params.
221 222 223 224 225 226 227 |
# File 'lib/ronin/core/params/mixin.rb', line 221 def initialize(*arguments, params: nil, **kwargs, &block) if params then self.params = params else initialize_params end super(*arguments,**kwargs,&block) end |
#initialize_params ⇒ Object
Initializes #params.
234 235 236 237 238 239 240 |
# File 'lib/ronin/core/params/mixin.rb', line 234 def initialize_params @params = {} self.class.params.each do |name,param| @params[name] = param.default_value if param.has_default? end end |
#set_param(name, value) ⇒ Object
Sets a param's value.
259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/ronin/core/params/mixin.rb', line 259 def set_param(name,value) unless (param = self.class.params[name]) raise(UnknownParam,"unknown param: #{name.inspect}") end @params[name] = begin param.coerce(value) rescue ValidationError => error raise(ValidationError,"invalid param value for param '#{name}': #{error.}") end end |
#validate_params ⇒ true
Validates that all required params are set.
305 306 307 308 309 310 311 312 313 |
# File 'lib/ronin/core/params/mixin.rb', line 305 def validate_params self.class.params.each do |name,param| if param.required? && @params[name].nil? raise(RequiredParam,"param '#{name}' requires a value") end end return true end |