Class: Porridge::FieldPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/porridge/field_policy.rb

Overview

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.

Direct Known Subclasses

WhitelistFieldPolicy

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ensure_valid!(*objects) ⇒ Boolean

Ensures that all the provided objects are valid field policies, raising InvalidFieldPolicyError if not.

Parameters:

  • objects (Array)

    the splatted array of objects to validate.

Returns:

  • (Boolean)

    true if all the objects were valid; raises an error otherwise.

Raises:



23
24
25
26
# 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) ⇒ Boolean

Determines whether the given object is a valid porridge field policy. Currently, any object that responds to the #allowed? method is valid.

Parameters:

  • object

    the object to check.

Returns:

  • (Boolean)

    true if the object is a valid field policy; false otherwise.



15
16
17
# File 'lib/porridge/field_policy.rb', line 15

def self.valid?(object)
  object.respond_to? :allowed?
end

Instance Method Details

#allowed?(_name, _object, _options) ⇒ Boolean

Determiners whether the field with the given name for the given object with the given options is currently allowed.

Parameters:

  • _name

    the name of the field being validated.

  • _object

    the object for which the field being validated is being generated.

  • _options (Hash)

    the options with which the field being validated is being generated.

Returns:

  • (Boolean)

    true if the indicated field is allowed; false otherwise.



34
35
36
# File 'lib/porridge/field_policy.rb', line 34

def allowed?(_name, _object, _options)
  true
end