Class: ResourcePolicy::Policy::AttributesPolicy::AttributeConfiguration

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#nameObject (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, **options)
  action_types.map(&:to_sym).each do |action|
    allowed_actions[action] = merged_action_options(action, options)
  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

Returns:

  • (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

Returns:

  • (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_actionsObject



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