Class: WSDSL::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:

Since:

  • 0.0.3

Defined Under Namespace

Classes: 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.

Since:

  • 0.0.3



68
69
70
# File 'lib/params.rb', line 68

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

Instance Attribute Details

#space_nameString (readonly)

The namespace used if any

Returns:

  • (String)

Since:

  • 0.0.3



63
64
65
# File 'lib/params.rb', line 63

def space_name
  @space_name
end

Instance Method Details

#array(name, options = {}) ⇒ Array<WSDSL::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:

  • (Array<WSDSL::Params::Rule>)

    List of optional or required param rules depending on the new param rule type

Since:

  • 0.0.3



249
250
251
# File 'lib/params.rb', line 249

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

#binary(name, options = {}) ⇒ Arrays<WSDSL::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:

  • (Arrays<WSDSL::Params::Rule>)

    List of optional or required param rules depending on the new param rule type

Since:

  • 0.0.3



232
233
234
# File 'lib/params.rb', line 232

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

#boolean(name, options = {}) ⇒ Arrays<WSDSL::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:

  • (Arrays<WSDSL::Params::Rule>)

    List of optional or required param rules depending on the new param rule type

Since:

  • 0.0.3



181
182
183
# File 'lib/params.rb', line 181

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

#datetime(name, options = {}) ⇒ Arrays<WSDSL::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:

  • (Arrays<WSDSL::Params::Rule>)

    List of optional or required param rules depending on the new param rule type

Since:

  • 0.0.3



198
199
200
# File 'lib/params.rb', line 198

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

#decimal(name, options = {}) ⇒ Arrays<WSDSL::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:

  • (Arrays<WSDSL::Params::Rule>)

    List of optional or required param rules depending on the new param rule type

Since:

  • 0.0.3



164
165
166
# File 'lib/params.rb', line 164

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

#file(name, options = {}) ⇒ Arrays<WSDSL::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:

  • (Arrays<WSDSL::Params::Rule>)

    List of optional or required param rules depending on the new param rule type

Since:

  • 0.0.3



266
267
268
# File 'lib/params.rb', line 266

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

#float(name, options = {}) ⇒ Arrays<WSDSL::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:

  • (Arrays<WSDSL::Params::Rule>)

    List of optional or required param rules depending on the new param rule type

Since:

  • 0.0.3



147
148
149
# File 'lib/params.rb', line 147

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

#integer(name, options = {}) ⇒ Arrays<WSDSL::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:

  • (Arrays<WSDSL::Params::Rule>)

    List of optional or required param rules depending on the new param rule type

Since:

  • 0.0.3



130
131
132
# File 'lib/params.rb', line 130

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

#list_optionalArray<WSDSL::Params::Rule>

Returns an array of all the optional params

Returns:

Since:

  • 0.0.3



335
336
337
# File 'lib/params.rb', line 335

def list_optional
  @optional ||= []
end

#list_requiredArray<WSDSL::Params::Rule>

Returns an array of all the required params

Returns:

Since:

  • 0.0.3



327
328
329
# File 'lib/params.rb', line 327

def list_required
  @required ||= []
end

#namespace(name) {|Params| ... } ⇒ Array<WSDSL::Params>

Defines a namespaced param

Yields:

  • (Params)

    the newly created namespaced param

Returns:

  • (Array<WSDSL::Params>)

    the list of all the namespaced params

Since:

  • 0.0.3



346
347
348
349
350
# File 'lib/params.rb', line 346

def namespace(name)
  params = Params.new(:space_name => name)
  yield(params) if block_given?
  namespaced_params << params unless namespaced_params.include?(params)
end

#namespaced_paramsArray<WSDSL::Params>

Returns the namespaced params

Returns:

  • (Array<WSDSL::Params>)

    the list of all the namespaced params

Since:

  • 0.0.3



356
357
358
# File 'lib/params.rb', line 356

def namespaced_params
  @namespaced_params ||= []
end

#optional(param_name, opts = {}) ⇒ Array<WSDSL::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:

Since:

  • 0.0.3



312
313
314
315
316
317
318
319
# File 'lib/params.rb', line 312

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)

Since:

  • 0.0.3



88
89
90
91
92
93
94
95
96
# File 'lib/params.rb', line 88

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<WSDSL::Params>

Returns the names of the first level expected params

Returns:

Since:

  • 0.0.3



364
365
366
367
368
# File 'lib/params.rb', line 364

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.to_s}
  first_level_expected_params
end

#required(param_name, opts = {}) ⇒ Array<WSDSL::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:

Since:

  • 0.0.3



286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/params.rb', line 286

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<WSDSL::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:

  • (Arrays<WSDSL::Params::Rule>)

    List of optional or required param rules depending on the new param rule type

Since:

  • 0.0.3



113
114
115
# File 'lib/params.rb', line 113

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

#text(name, options = {}) ⇒ Arrays<WSDSL::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:

  • (Arrays<WSDSL::Params::Rule>)

    List of optional or required param rules depending on the new param rule type

Since:

  • 0.0.3



215
216
217
# File 'lib/params.rb', line 215

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