class Porridge::FieldPolicy

{FieldPolicy} is the nominal base class for all field policy classes.

A field policy is an object that is capable of determining whether a certain “field” is allowed in a given context. Currently, it is primarily used in {FieldSerializer} as the default method of determining whether a field is valid. You are encouraged, but not required, to have your own custom field policies derive from this class. Currently, any object that implements the #allowed? method is a valid field policy.

Public Class Methods

ensure_valid!(*objects) click to toggle source

Ensures that all the provided objects are valid field policies, raising {InvalidFieldPolicyError} if not. @param objects [Array] the splatted array of objects to validate. @return [Boolean] true if all the objects were valid; raises an error otherwise. @raise [InvalidFieldPolicyError] if any of the provided objects are not valid field policies.

# File lib/porridge/field_policy.rb, line 23
def self.ensure_valid!(*objects)
  objects.each { |object| raise InvalidFieldPolicyError unless valid?(object) }
  true
end
valid?(object) click to toggle source

Determines whether the given object is a valid porridge field policy. Currently, any object that responds to the #allowed? method is valid. @param object the object to check. @return [Boolean] true if the object is a valid field policy; false otherwise.

# File lib/porridge/field_policy.rb, line 15
def self.valid?(object)
  object.respond_to? :allowed?
end

Public Instance Methods

allowed?(_name, _object, _options) click to toggle source

Determiners whether the field with the given name for the given object with the given options is currently allowed. @param _name the name of the field being validated. @param _object the object for which the field being validated is being generated. @param _options [Hash] the options with which the field being validated is being generated. @return [Boolean] true if the indicated field is allowed; false otherwise.

# File lib/porridge/field_policy.rb, line 34
def allowed?(_name, _object, _options)
  true
end