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

#&, #|

Constructor Details

#initialize(constraints) ⇒ HashConstraint

Returns a new instance of HashConstraint.



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

def initialize(constraints)
  @constraints = constraints.with_indifferent_access
end

Instance Attribute Details

#constraintsObject (readonly)

Returns the value of attribute constraints.



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

def constraints
  @constraints
end

Instance Method Details

#==(other) ⇒ Object



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

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

#merge(other) ⇒ Object



19
20
21
22
# File 'lib/stronger_parameters/constraints/hash_constraint.rb', line 19

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

#value(v) ⇒ Object

Raises:



11
12
13
14
15
16
17
# File 'lib/stronger_parameters/constraints/hash_constraint.rb', line 11

def value(v)
  if v.is_a?(Hash)
    return ActionController::Parameters.new(v).permit(constraints)
  end

  raise InvalidParameter.new(v, "must be a hash")
end