Class: ResourcePolicy::Policy::AttributesPolicy::AttributeConfiguration
- Inherits:
-
Object
- Object
- ResourcePolicy::Policy::AttributesPolicy::AttributeConfiguration
- Defined in:
- lib/resource_policy/policy/attributes_policy/attribute_configuration.rb
Overview
Allows to define policy for single attribute
Constant Summary collapse
- DEFAULT_OPTIONS =
{ if: [] }.freeze
- ALLOWED_ACTIONS =
%i[read write].freeze
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #allowed(*action_types, **options) ⇒ Object
- #conditions_for(action) ⇒ Object
- #configured? ⇒ Boolean
- #defined_action?(action_name) ⇒ Boolean
- #defined_actions ⇒ Object
-
#initialize(name, policy_configuration:) ⇒ AttributeConfiguration
constructor
A new instance of AttributeConfiguration.
- #initialize_copy(other) ⇒ Object
- #merge(other) ⇒ Object
Constructor Details
#initialize(name, policy_configuration:) ⇒ AttributeConfiguration
Returns a new instance of AttributeConfiguration.
15 16 17 18 19 |
# File 'lib/resource_policy/policy/attributes_policy/attribute_configuration.rb', line 15 def initialize(name, policy_configuration:) @name = name @allowed_actions = {} @policy_configuration = policy_configuration end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'lib/resource_policy/policy/attributes_policy/attribute_configuration.rb', line 13 def name @name end |
Instance Method Details
#allowed(*action_types, **options) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/resource_policy/policy/attributes_policy/attribute_configuration.rb', line 26 def allowed(*action_types, **) action_types.map(&:to_sym).each do |action| allowed_actions[action] = (action, ) end self end |
#conditions_for(action) ⇒ Object
33 34 35 36 |
# File 'lib/resource_policy/policy/attributes_policy/attribute_configuration.rb', line 33 def conditions_for(action) action_conditions = allowed_actions.fetch(action, {}).fetch(:if, []) (action_conditions + policy_configuration.group_conditions).uniq end |
#configured? ⇒ Boolean
38 39 40 |
# File 'lib/resource_policy/policy/attributes_policy/attribute_configuration.rb', line 38 def configured? !defined_actions.empty? end |
#defined_action?(action_name) ⇒ Boolean
46 47 48 |
# File 'lib/resource_policy/policy/attributes_policy/attribute_configuration.rb', line 46 def defined_action?(action_name) defined_actions.include?(action_name.to_sym) end |
#defined_actions ⇒ Object
42 43 44 |
# File 'lib/resource_policy/policy/attributes_policy/attribute_configuration.rb', line 42 def defined_actions allowed_actions.keys end |
#initialize_copy(other) ⇒ Object
21 22 23 24 |
# File 'lib/resource_policy/policy/attributes_policy/attribute_configuration.rb', line 21 def initialize_copy(other) super @allowed_actions = @allowed_actions.dup.transform_values(&:dup) end |
#merge(other) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/resource_policy/policy/attributes_policy/attribute_configuration.rb', line 50 def merge(other) dup.tap do |new_attribute| other.defined_actions.each do |action| new_attribute.allowed(action, if: other.conditions_for(action)) end end end |