Class: Grape::Validations::ParamsScope

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api, element, parent, &block) ⇒ ParamsScope

Returns a new instance of ParamsScope.



77
78
79
80
81
82
# File 'lib/grape/validations.rb', line 77

def initialize(api, element, parent, &block)
  @element = element
  @parent = parent
  @api = api
  instance_eval(&block)
end

Instance Attribute Details

#elementObject

Returns the value of attribute element.



75
76
77
# File 'lib/grape/validations.rb', line 75

def element
  @element
end

#parentObject

Returns the value of attribute parent.



75
76
77
# File 'lib/grape/validations.rb', line 75

def parent
  @parent
end

Instance Method Details

#full_name(name) ⇒ Object



114
115
116
117
# File 'lib/grape/validations.rb', line 114

def full_name(name)
  return "#{@parent.full_name(@element)}[#{name}]" if @parent
  name.to_s
end

#group(element, &block) ⇒ Object



104
105
106
# File 'lib/grape/validations.rb', line 104

def group(element, &block)
  ParamsScope.new(@api, element, self, &block)
end

#optional(*attrs) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/grape/validations.rb', line 94

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



108
109
110
111
112
# File 'lib/grape/validations.rb', line 108

def params(params)
  params = @parent.params(params) if @parent
  params = params[@element] || {} if @element
  params
end

#requires(*attrs) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/grape/validations.rb', line 84

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