Class: Grape::Validations::ParamsScope
- Inherits:
-
Object
- Object
- Grape::Validations::ParamsScope
- Defined in:
- lib/grape/validations.rb
Instance Attribute Summary collapse
-
#element ⇒ Object
Returns the value of attribute element.
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
- #full_name(name) ⇒ Object
- #group(element, &block) ⇒ Object
-
#initialize(api, element, parent, &block) ⇒ ParamsScope
constructor
A new instance of ParamsScope.
- #optional(*attrs) ⇒ Object
- #params(params) ⇒ Object
- #requires(*attrs) ⇒ Object
Constructor Details
#initialize(api, element, parent, &block) ⇒ ParamsScope
Returns a new instance of ParamsScope.
84 85 86 87 88 89 |
# File 'lib/grape/validations.rb', line 84 def initialize(api, element, parent, &block) @element = element @parent = parent @api = api instance_eval(&block) end |
Instance Attribute Details
#element ⇒ Object
Returns the value of attribute element.
82 83 84 |
# File 'lib/grape/validations.rb', line 82 def element @element end |
#parent ⇒ Object
Returns the value of attribute parent.
82 83 84 |
# File 'lib/grape/validations.rb', line 82 def parent @parent end |
Instance Method Details
#full_name(name) ⇒ Object
121 122 123 124 |
# File 'lib/grape/validations.rb', line 121 def full_name(name) return "#{@parent.full_name(@element)}[#{name}]" if @parent name.to_s end |
#group(element, &block) ⇒ Object
111 112 113 |
# File 'lib/grape/validations.rb', line 111 def group(element, &block) ParamsScope.new(@api, element, self, &block) end |
#optional(*attrs) ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/grape/validations.rb', line 101 def optional(*attrs) validations = {} if attrs.last.is_a?(Hash) validations.merge!(attrs.pop) end push_declared_params(attrs) validates(attrs, validations) end |
#params(params) ⇒ Object
115 116 117 118 119 |
# File 'lib/grape/validations.rb', line 115 def params(params) params = @parent.params(params) if @parent params = params[@element] || {} if @element params end |
#requires(*attrs) ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/grape/validations.rb', line 91 def requires(*attrs) validations = {:presence => true} if attrs.last.is_a?(Hash) validations.merge!(attrs.pop) end push_declared_params(attrs) validates(attrs, validations) end |