Class: Porridge::WhitelistFieldPolicy
- Inherits:
-
FieldPolicy
- Object
- FieldPolicy
- Porridge::WhitelistFieldPolicy
- 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
-
#whitelist ⇒ Hash
readonly
private
The nested whitelist hash of field names.
Instance Method Summary collapse
- #_allowed?(field_hierarchy, whitelist, level = 0) ⇒ Boolean private
-
#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.
-
#hash?(input) ⇒ Boolean
protected
Determines whether the given object functions as a hash for the purposes of this WhitelistFieldPolicy instance.
-
#initialize(whitelist) ⇒ WhitelistFieldPolicy
constructor
Creates a new instance of WhitelistFieldPolicy with the given whitelist.
Methods inherited from FieldPolicy
Constructor Details
#initialize(whitelist) ⇒ WhitelistFieldPolicy
Creates a new instance of Porridge::WhitelistFieldPolicy with the given whitelist.
9 10 11 12 |
# File 'lib/porridge/whitelist_field_policy.rb', line 9 def initialize(whitelist) @whitelist = whitelist super() end |
Instance Attribute Details
#whitelist ⇒ Hash (readonly, private)
The nested whitelist hash of field names.
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)
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.
20 21 22 23 |
# File 'lib/porridge/whitelist_field_policy.rb', line 20 def allowed?(name, _object, ) field_hierarchy = [: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 #[]
.
31 32 33 |
# File 'lib/porridge/whitelist_field_policy.rb', line 31 def hash?(input) input.is_a? Hash end |