Class: Porridge::WhitelistFieldPolicy

Inherits:
FieldPolicy show all
Defined in:
lib/porridge/whitelist_field_policy.rb

Overview

WhitelistFieldPolicy is a field policy that uses a nested whitelist of field names to determine which fields are valid.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from FieldPolicy

ensure_valid!, valid?

Constructor Details

#initialize(whitelist) ⇒ WhitelistFieldPolicy

Creates a new instance of Porridge::WhitelistFieldPolicy with the given whitelist.

Parameters:

  • whitelist (Hash)

    the nested whitelist hash of allowed field names.



9
10
11
12
# File 'lib/porridge/whitelist_field_policy.rb', line 9

def initialize(whitelist)
  @whitelist = whitelist
  super()
end

Instance Attribute Details

#whitelistHash (readonly, private)

The nested whitelist hash of field names.

Returns:

  • (Hash)


68
69
70
# File 'lib/porridge/whitelist_field_policy.rb', line 68

def whitelist
  @whitelist
end

Instance Method Details

#_allowed?(field_hierarchy, whitelist) ⇒ Boolean (private) #_allowed?(field_hierarchy, whitelist, level) ⇒ Boolean (private)

Overloads:

  • #_allowed?(field_hierarchy, whitelist) ⇒ Boolean

    Recursively traverses the given field hierarchy and determines whether the field indicated by the hierarchy is allowed for the given whitelist.

    Parameters:

    • field_hierarchy (Array)

      the field hierarchy to validate.

    • whitelist (Hash)

      the nested whitelist hash of field names.

  • #_allowed?(field_hierarchy, whitelist, level) ⇒ Boolean

    Recursively traverses the given field hierarchy and determines whether the field indicated by the hierarchy is allowed for the given whitelist, starting from the specified level.

    Parameters:

    • field_hierarchy (Array)

      the field hierarchy to validate.

    • whitelist (Hash)

      the nested whitelist hash of field names.

    • level (Integer)

      the current level of the hierarchy being checked.

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/porridge/whitelist_field_policy.rb', line 48

def _allowed?(field_hierarchy, whitelist, level = 0)
  # If the level is equal to the field hierarchy length, then we've reached the end. Immediately return the
  # truthiness of whitelist, which is now equal to the final resolved value referenced by the field hierarchy.
  return !!whitelist if level >= field_hierarchy.count

  # If the current whitelist is not a hash, then the field hierarchy is deeper than the whitelist.
  # As an example, take this whitelist:
  #   { users: true }
  # And this field hierarchy:
  #   [:user, :id]
  # One interpretation of this is that since 'users' is true, all fields should be allowed. We take the opposite
  # approach and say that no attributes have been explicitly defined.
  # Therefore immediately return false.
  return false unless hash?(whitelist)

  _allowed?(field_hierarchy, whitelist[field_hierarchy[level]], level + 1)
end

#allowed?(name, _object, options) ⇒ Boolean

Determiners whether the field with the given name with the given options is currently allowed by checking the field hierarchy, which must be contained in +options against the whitelist.

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.



20
21
22
23
# File 'lib/porridge/whitelist_field_policy.rb', line 20

def allowed?(name, _object, options)
  field_hierarchy = options[:field_hierarchy] || []
  _allowed?([*field_hierarchy, name], whitelist)
end

#hash?(input) ⇒ Boolean (protected)

Determines whether the given object functions as a hash for the purposes of this Porridge::WhitelistFieldPolicy instance. You may override this method if desired, but hashes must at least respond to #[].

Parameters:

  • input

    the input object to check.

Returns:

  • (Boolean)

    true if the given object is like a hash; false otherwise.



31
32
33
# File 'lib/porridge/whitelist_field_policy.rb', line 31

def hash?(input)
  input.is_a? Hash
end