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



71
72
73
# File 'lib/params.rb', line 71

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



66
67
68
# File 'lib/params.rb', line 66

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



252
253
254
# File 'lib/params.rb', line 252

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



235
236
237
# File 'lib/params.rb', line 235

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



184
185
186
# File 'lib/params.rb', line 184

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



201
202
203
# File 'lib/params.rb', line 201

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



167
168
169
# File 'lib/params.rb', line 167

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



269
270
271
# File 'lib/params.rb', line 269

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



150
151
152
# File 'lib/params.rb', line 150

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



133
134
135
# File 'lib/params.rb', line 133

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



338
339
340
# File 'lib/params.rb', line 338

def list_optional
  @optional ||= []
end

#list_requiredArray<WSDSL::Params::Rule>

Returns an array of all the required params

Returns:

Since:

  • 0.0.3



330
331
332
# File 'lib/params.rb', line 330

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



349
350
351
352
353
# File 'lib/params.rb', line 349

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



359
360
361
# File 'lib/params.rb', line 359

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



315
316
317
318
319
320
321
322
# File 'lib/params.rb', line 315

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



91
92
93
94
95
96
97
98
99
# File 'lib/params.rb', line 91

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



367
368
369
370
371
# File 'lib/params.rb', line 367

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



289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/params.rb', line 289

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



116
117
118
# File 'lib/params.rb', line 116

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



218
219
220
# File 'lib/params.rb', line 218

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