module ActionComponent::Constraints
Constants
- Constraint
Public Instance Methods
optional(fields)
click to toggle source
# File lib/action_component/constraints.rb, line 29 def optional(fields) raise ActionComponent::ConstraintError, "optional can only take a hash of field names and classes" unless fields.is_a?(Hash) fields.each do |name, class_constraint| self.initializer_constraints += [Constraint.new(name, class_constraint, false)] end end
required(*fields)
click to toggle source
# File lib/action_component/constraints.rb, line 17 def required(*fields) fields.each do |field| if field.is_a?(Hash) field.each do |name, class_constraint| self.initializer_constraints += [Constraint.new(name, class_constraint, true)] end else self.initializer_constraints += [Constraint.new(field, nil, true)] end end end
Private Instance Methods
check_constraints!(opts)
click to toggle source
# File lib/action_component/constraints.rb, line 40 def check_constraints!(opts) initializer_constraints.each do |constraint| if constraint.required? && !opts.member?(constraint.name) raise ActionComponent::ConstraintError, "#{constraint.name} is required for component #{self.class.name}" end if constraint.class_constraint && opts.member?(constraint.name) && !opts[constraint.name].is_a?(constraint.class_constraint) raise ActionComponent::ConstraintError, "#{constraint.name} must be a #{constraint.class_constraint.name}" end end end