Class: WeaselDiesel::Params

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

Overview

Service params class letting you define param rules. Usually not initialized directly but accessed via the service methods.

See Also:

Defined Under Namespace

Classes: Namespace, Rule

Instance Attribute Summary collapse

Params defintition DSL (accept_param style) collapse

param setters based on the state (required or optional) collapse

params accessors per status (required or optional) collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Params

Returns a new instance of Params.

Parameters:

  • opts (Hash) (defaults to: {})

    The params options

Options Hash (opts):

  • :space_name (:symbol)

    Optional namespace.



109
110
111
# File 'lib/params.rb', line 109

def initialize(opts={})
  @space_name = opts[:space_name]
end

Instance Attribute Details

#space_nameNilClass, WeaselDiesel::Params::Namespace (readonly)

The namespace used if any

Returns:



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

Examples:

Defining a string service param named type which has various options.

service.param.string  :type, :in => LeaderboardType.names, :default => LeaderboardType::LIFETIME

Parameters:

  • name (String)

    The name of the param

  • options (Hash) (defaults to: {})

    A hash representing the param settings

Returns:



290
291
292
# File 'lib/params.rb', line 290

def array(name, options={})
  param(:array, name, options)
end

#binary(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>

Defines a new binary param and add it to the required or optional list

Examples:

Defining a string service param named type which has various options.

service.param.string  :type, :in => LeaderboardType.names, :default => LeaderboardType::LIFETIME

Parameters:

  • name (String)

    The name of the param

  • options (Hash) (defaults to: {})

    A hash representing the param settings

Returns:



273
274
275
# File 'lib/params.rb', line 273

def binary(name, options={})
  param(:binary, name, options)
end

#boolean(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>

Defines a new boolean param and add it to the required or optional list

Examples:

Defining a string service param named type which has various options.

service.param.string  :type, :in => LeaderboardType.names, :default => LeaderboardType::LIFETIME

Parameters:

  • name (String)

    The name of the param

  • options (Hash) (defaults to: {})

    A hash representing the param settings

Returns:



222
223
224
# File 'lib/params.rb', line 222

def boolean(name, options={})
  param(:boolean, name, options)
end

#datetime(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>

Defines a new datetime param and add it to the required or optional list

Examples:

Defining a string service param named type which has various options.

service.param.string  :type, :in => LeaderboardType.names, :default => LeaderboardType::LIFETIME

Parameters:

  • name (String)

    The name of the param

  • options (Hash) (defaults to: {})

    A hash representing the param settings

Returns:



239
240
241
# File 'lib/params.rb', line 239

def datetime(name, options={})
  param(:datetime, name, options)
end

#decimal(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>

Defines a new decimal param and add it to the required or optional list

Examples:

Defining a string service param named type which has various options.

service.param.string  :type, :in => LeaderboardType.names, :default => LeaderboardType::LIFETIME

Parameters:

  • name (String)

    The name of the param

  • options (Hash) (defaults to: {})

    A hash representing the param settings

Returns:



205
206
207
# File 'lib/params.rb', line 205

def decimal(name, options={})
  param(:decimal, name, options)
end

#file(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>

Defines a new file param and add it to the required or optional list

Examples:

Defining a string service param named type which has various options.

service.param.string  :type, :in => LeaderboardType.names, :default => LeaderboardType::LIFETIME

Parameters:

  • name (String)

    The name of the param

  • options (Hash) (defaults to: {})

    A hash representing the param settings

Returns:



307
308
309
# File 'lib/params.rb', line 307

def file(name, options={})
  param(:file, name, options)
end

#float(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>

Defines a new float param and add it to the required or optional list

Examples:

Defining a string service param named type which has various options.

service.param.string  :type, :in => LeaderboardType.names, :default => LeaderboardType::LIFETIME

Parameters:

  • name (String)

    The name of the param

  • options (Hash) (defaults to: {})

    A hash representing the param settings

Returns:



188
189
190
# File 'lib/params.rb', line 188

def float(name, options={})
  param(:float, name, options)
end

#integer(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>

Defines a new integer param and add it to the required or optional list

Examples:

Defining a string service param named type which has various options.

service.param.string  :type, :in => LeaderboardType.names, :default => LeaderboardType::LIFETIME

Parameters:

  • name (String)

    The name of the param

  • options (Hash) (defaults to: {})

    A hash representing the param settings

Returns:



171
172
173
# File 'lib/params.rb', line 171

def integer(name, options={})
  param(:integer, name, options)
end

#list_optionalArray<WeaselDiesel::Params::Rule>

Returns an array of all the optional params

Returns:



376
377
378
# File 'lib/params.rb', line 376

def list_optional
  @optional ||= []
end

#list_requiredArray<WeaselDiesel::Params::Rule>

Returns an array of all the required params

Returns:



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

Parameters:

  • name (Symbol, String)

    The name of the namespace

  • opts (Hash) (defaults to: {})

    A hash representing the namespace settings

Yields:

  • (Params)

    the newly created namespaced param

Returns:



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_paramsArray<WeaselDiesel::Params>

Returns the namespaced params

Returns:



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

Examples:

Defining an optional service param called ‘id’ of ‘Integer` type

service.params.optional :id, :type => 'integer', :default => 9999

Parameters:

  • param_name (Symbol, String)

    The name of the param to define

  • opts (Hash) (defaults to: {})

    A hash representing the required param, the key being the param name name and the value being a hash of options.

Returns:



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.

Examples:

Declaring an integer service param called id

service.param(:id, :integer, :default => 9999, :in => [0, 9999])

Parameters:

  • type (Symbol)

    The type of param

  • name (Symbol, String)

    The name of the param

  • options (Hash) (defaults to: {})

    A hash representing the param settings

Returns:

  • (Array)

    the typed list of params (required or optional)



129
130
131
132
133
134
135
136
137
# File 'lib/params.rb', line 129

def param(type, name, options={})
  options[:type] = type
  options[:space_name] = options[:space_name] || space_name
  if options.delete(:required)
    list_required << Rule.new(name, options)
  else
    list_optional << Rule.new(name, options)
  end
end

#param_namesArray<WeaselDiesel::Params>

Returns the names of the first level expected params

Returns:



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

Examples:

Defining a required service param called ‘id’ of ‘Integer` type

service.params.required :id, :type => 'integer', :default => 9999

Parameters:

  • param_name (Symbol, String)

    The name of the param to define

  • opts (Hash) (defaults to: {})

    A hash representing the required param, the key being the param name name and the value being a hash of options.

Returns:



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

Examples:

Defining a string service param named type which has various options.

service.param.string  :type, :in => LeaderboardType.names, :default => LeaderboardType::LIFETIME

Parameters:

  • name (String)

    The name of the param

  • options (Hash) (defaults to: {})

    A hash representing the param settings

Returns:



154
155
156
# File 'lib/params.rb', line 154

def string(name, options={})
  param(:string, name, options)
end

#text(name, options = {}) ⇒ Arrays<WeaselDiesel::Params::Rule>

Defines a new text param and add it to the required or optional list

Examples:

Defining a string service param named type which has various options.

service.param.string  :type, :in => LeaderboardType.names, :default => LeaderboardType::LIFETIME

Parameters:

  • name (String)

    The name of the param

  • options (Hash) (defaults to: {})

    A hash representing the param settings

Returns:



256
257
258
# File 'lib/params.rb', line 256

def text(name, options={})
  param(:text, name, options)
end