Class: WeaselDiesel::Params::Rule

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

Overview

Params usually have a few rules used to validate requests. Rules are not usually initialized directly but instead via the service’s #params accessor.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ Rule

Returns a new instance of Rule.

Parameters:

  • name (Symbol, String)

    The param’s name

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

    The rule options

Options Hash (opts):

  • :in (Symbol)

    A list of acceptable values.

  • :options (Symbol)

    A list of acceptable values.

  • :default (Symbol)

    The default value of the param.

  • :min_value (Symbol)

    The minimum acceptable value.

  • :max_value (Symbol)

    The maximum acceptable value.

  • :min_length (Symbol)

    The minimum acceptable string length.

  • :max_length (Symbol)

    The maximum acceptable string length.

  • :null (Boolean)

    Can this value be null?

  • :doc (Symbol)

    Documentation for the param.



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

def initialize(name, opts = {})
  @name    = name
  @options = opts
end

Instance Attribute Details

#nameSymbol, String (readonly)

Returns name The name of the param the rule applies to.

Returns:

  • (Symbol, String)

    name The name of the param the rule applies to.



42
43
44
# File 'lib/params.rb', line 42

def name
  @name
end

#optionsHash (readonly)

Returns options The rule options.

Returns:

  • (Hash)

    options The rule options.



55
56
57
# File 'lib/params.rb', line 55

def options
  @options
end

Instance Method Details

#docNilClass, String

The documentation of this Rule

api public

Returns:

  • (NilClass, String)


87
88
89
# File 'lib/params.rb', line 87

def doc
  @options[:doc]
end

#namespaceNilClass, WeaselDiesel::Params::Namespace

The namespace used if any

Returns:



79
80
81
# File 'lib/params.rb', line 79

def namespace
  @options[:space_name]
end

#to_hashHash

Converts the rule into a hash with its name and options.

Returns:

  • (Hash)


94
95
96
# File 'lib/params.rb', line 94

def to_hash
  {:name => name, :options => options}
end