Class: StrongerParameters::HashConstraint

Inherits:
Constraint
  • Object
show all
Defined in:
lib/stronger_parameters/constraints/hash_constraint.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Constraint

#&, #required, #required?, #|

Constructor Details

#initialize(constraints) ⇒ HashConstraint

Returns a new instance of HashConstraint.



8
9
10
# File 'lib/stronger_parameters/constraints/hash_constraint.rb', line 8

def initialize(constraints)
  @constraints = constraints.with_indifferent_access unless constraints.nil?
end

Instance Attribute Details

#constraintsObject (readonly)

Returns the value of attribute constraints.



6
7
8
# File 'lib/stronger_parameters/constraints/hash_constraint.rb', line 6

def constraints
  @constraints
end

Instance Method Details

#==(other) ⇒ Object



28
29
30
# File 'lib/stronger_parameters/constraints/hash_constraint.rb', line 28

def ==(other)
  super && constraints == other.constraints
end

#merge(other) ⇒ Object



23
24
25
26
# File 'lib/stronger_parameters/constraints/hash_constraint.rb', line 23

def merge(other)
  other_constraints = other.is_a?(HashConstraint) ? other.constraints : other
  self.class.new(constraints.merge(other_constraints))
end

#value(v) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/stronger_parameters/constraints/hash_constraint.rb', line 12

def value(v)
  return InvalidValue.new(v, "must be a hash") if !v.is_a?(Hash) && !v.is_a?(ActionController::Parameters)

  v = ActionController::Parameters.new(v) if v.is_a?(Hash)
  if constraints.nil?
    v.permit!
  else
    v.permit(constraints)
  end
end