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
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
#constraints ⇒ Object (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
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
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/stronger_parameters/constraints/hash_constraint.rb', line 12 def value(v) if v.is_a?(Hash) return ActionController::Parameters.new(v).permit! if constraints.nil? return ActionController::Parameters.new(v).permit(constraints) elsif ActionPack::VERSION::MAJOR >= 5 && v.is_a?(ActionController::Parameters) return v.permit! if constraints.nil? return v.permit(constraints) end InvalidValue.new(v, "must be a hash") end |