Class: StrongerParameters::HashConstraint
- Inherits:
-
Constraint
- Object
- Constraint
- StrongerParameters::HashConstraint
- Defined in:
- lib/stronger_parameters/constraints/hash_constraint.rb
Instance Attribute Summary collapse
-
#constraints ⇒ Object
readonly
Returns the value of attribute constraints.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(constraints) ⇒ HashConstraint
constructor
A new instance of HashConstraint.
- #merge(other) ⇒ Object
- #value(v) ⇒ Object
Methods inherited from Constraint
#&, #required, #required?, #|
Constructor Details
#initialize(constraints) ⇒ HashConstraint
Returns a new instance of HashConstraint.
9 10 11 |
# File 'lib/stronger_parameters/constraints/hash_constraint.rb', line 9 def initialize(constraints) @constraints = constraints.with_indifferent_access unless constraints.nil? end |
Instance Attribute Details
#constraints ⇒ Object (readonly)
Returns the value of attribute constraints.
7 8 9 |
# File 'lib/stronger_parameters/constraints/hash_constraint.rb', line 7 def constraints @constraints end |
Instance Method Details
#==(other) ⇒ Object
29 30 31 |
# File 'lib/stronger_parameters/constraints/hash_constraint.rb', line 29 def ==(other) super && constraints == other.constraints end |
#merge(other) ⇒ Object
24 25 26 27 |
# File 'lib/stronger_parameters/constraints/hash_constraint.rb', line 24 def merge(other) other_constraints = other.is_a?(HashConstraint) ? other.constraints : other self.class.new(constraints.merge(other_constraints)) end |
#value(v) ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/stronger_parameters/constraints/hash_constraint.rb', line 13 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 |