Class: WeaselDiesel::Params
- Inherits:
-
Object
- Object
- WeaselDiesel::Params
- Defined in:
- lib/params.rb
Overview
Service params class letting you define param rules. Usually not initialized directly but accessed via the service methods.
Defined Under Namespace
Instance Attribute Summary collapse
-
#space_name ⇒ NilClass, WeaselDiesel::Params::Namespace
readonly
The namespace used if any.
Params defintition DSL (accept_param style) collapse
-
#array(name, options = {}) ⇒ Array<WeaselDiesel::Params::Rule>
Defines a new array param and add it to the required or optional list.
-
#binary(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>
Defines a new binary param and add it to the required or optional list.
-
#boolean(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>
Defines a new boolean param and add it to the required or optional list.
-
#datetime(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>
Defines a new datetime param and add it to the required or optional list.
-
#decimal(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>
Defines a new decimal param and add it to the required or optional list.
-
#file(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>
Defines a new file param and add it to the required or optional list.
-
#float(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>
Defines a new float param and add it to the required or optional list.
-
#integer(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>
Defines a new integer param and add it to the required or optional list.
-
#string(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>
Defines a new string param and add it to the required or optional list.
-
#text(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>
Defines a new text param and add it to the required or optional list.
param setters based on the state (required or optional) collapse
-
#optional(param_name, opts = {}) ⇒ Array<WeaselDiesel::Params::Rule>
Defines a new optional param rule.
-
#required(param_name, opts = {}) ⇒ Array<WeaselDiesel::Params::Rule>
Defines a new required param.
params accessors per status (required or optional) collapse
-
#list_optional ⇒ Array<WeaselDiesel::Params::Rule>
Returns an array of all the optional params.
-
#list_required ⇒ Array<WeaselDiesel::Params::Rule>
Returns an array of all the required params.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Params
constructor
A new instance of Params.
-
#namespace(name, opts = {}) {|Params| ... } ⇒ Array<WeaselDiesel::Params>
(also: #object)
Defines a namespaced param.
-
#namespaced_params ⇒ Array<WeaselDiesel::Params>
Returns the namespaced params.
-
#param(type, name, options = {}) ⇒ Array
Defines a new param and add it to the optional or required list based the passed options.
-
#param_names ⇒ Array<WeaselDiesel::Params>
Returns the names of the first level expected params.
Constructor Details
#initialize(opts = {}) ⇒ Params
Returns a new instance of Params.
109 110 111 |
# File 'lib/params.rb', line 109 def initialize(opts={}) @space_name = opts[:space_name] end |
Instance Attribute Details
#space_name ⇒ NilClass, WeaselDiesel::Params::Namespace (readonly)
The namespace used if any
104 105 106 |
# File 'lib/params.rb', line 104 def space_name @space_name end |
Instance Method Details
#array(name, options = {}) ⇒ Array<WeaselDiesel::Params::Rule>
Defines a new array param and add it to the required or optional list
290 291 292 |
# File 'lib/params.rb', line 290 def array(name, ={}) param(:array, name, ) end |
#binary(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>
Defines a new binary param and add it to the required or optional list
273 274 275 |
# File 'lib/params.rb', line 273 def binary(name, ={}) param(:binary, name, ) end |
#boolean(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>
Defines a new boolean param and add it to the required or optional list
222 223 224 |
# File 'lib/params.rb', line 222 def boolean(name, ={}) param(:boolean, name, ) end |
#datetime(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>
Defines a new datetime param and add it to the required or optional list
239 240 241 |
# File 'lib/params.rb', line 239 def datetime(name, ={}) param(:datetime, name, ) end |
#decimal(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>
Defines a new decimal param and add it to the required or optional list
205 206 207 |
# File 'lib/params.rb', line 205 def decimal(name, ={}) param(:decimal, name, ) end |
#file(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>
Defines a new file param and add it to the required or optional list
307 308 309 |
# File 'lib/params.rb', line 307 def file(name, ={}) param(:file, name, ) end |
#float(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>
Defines a new float param and add it to the required or optional list
188 189 190 |
# File 'lib/params.rb', line 188 def float(name, ={}) param(:float, name, ) end |
#integer(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>
Defines a new integer param and add it to the required or optional list
171 172 173 |
# File 'lib/params.rb', line 171 def integer(name, ={}) param(:integer, name, ) end |
#list_optional ⇒ Array<WeaselDiesel::Params::Rule>
Returns an array of all the optional params
376 377 378 |
# File 'lib/params.rb', line 376 def list_optional @optional ||= [] end |
#list_required ⇒ Array<WeaselDiesel::Params::Rule>
Returns an array of all the required params
368 369 370 |
# File 'lib/params.rb', line 368 def list_required @required ||= [] end |
#namespace(name, opts = {}) {|Params| ... } ⇒ Array<WeaselDiesel::Params> Also known as: object
Defines a namespaced param
392 393 394 395 396 |
# File 'lib/params.rb', line 392 def namespace(name, opts={}) params = Params.new(:space_name => Namespace.new(name, :null => opts[:null])) yield(params) if block_given? namespaced_params << params unless namespaced_params.include?(params) end |
#namespaced_params ⇒ Array<WeaselDiesel::Params>
Returns the namespaced params
403 404 405 |
# File 'lib/params.rb', line 403 def namespaced_params @namespaced_params ||= [] end |
#optional(param_name, opts = {}) ⇒ Array<WeaselDiesel::Params::Rule>
Defines a new optional param rule
353 354 355 356 357 358 359 360 |
# File 'lib/params.rb', line 353 def optional(param_name, opts={}) # # recursive rule creation # if opts.size > 1 # opts.each_pair{|k,v| optional({k => v})} # else list_optional << Rule.new(param_name, opts) # end end |
#param(type, name, options = {}) ⇒ Array
Defines a new param and add it to the optional or required list based the passed options.
129 130 131 132 133 134 135 136 137 |
# File 'lib/params.rb', line 129 def param(type, name, ={}) [:type] = type [:space_name] = [:space_name] || space_name if .delete(:required) list_required << Rule.new(name, ) else list_optional << Rule.new(name, ) end end |
#param_names ⇒ Array<WeaselDiesel::Params>
Returns the names of the first level expected params
411 412 413 414 415 |
# File 'lib/params.rb', line 411 def param_names first_level_expected_params = (list_required + list_optional).map{|rule| rule.name.to_s} first_level_expected_params += namespaced_params.map{|r| r.space_name.name.to_s} first_level_expected_params end |
#required(param_name, opts = {}) ⇒ Array<WeaselDiesel::Params::Rule>
Defines a new required param
327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/params.rb', line 327 def required(param_name, opts={}) # # support for when a required param doesn't have any options # unless opts.respond_to?(:each_pair) # opts = {opts => nil} # end # # recursive rule creation # if opts.size > 1 # opts.each_pair{|k,v| requires({k => v})} # else list_required << Rule.new(param_name, opts) # end end |
#string(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>
Defines a new string param and add it to the required or optional list
154 155 156 |
# File 'lib/params.rb', line 154 def string(name, ={}) param(:string, name, ) end |
#text(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>
Defines a new text param and add it to the required or optional list
256 257 258 |
# File 'lib/params.rb', line 256 def text(name, ={}) param(:text, name, ) end |