Class: ResourcePolicy::Policy::PolicyConfiguration

Inherits:
Object
  • Object
show all
Defined in:
lib/resource_policy/policy/policy_configuration.rb

Overview

Stores all configuration about policy

Constant Summary collapse

EmptyConfiguration =
Class.new do
  def group_conditions
    []
  end
end
EMPTY_CONFIGURATION =
EmptyConfiguration.new

Instance Method Summary collapse

Constructor Details

#initialize(parent_configuration: EMPTY_CONFIGURATION, extra_group_conditions: []) ⇒ PolicyConfiguration

Returns a new instance of PolicyConfiguration.



20
21
22
23
24
25
# File 'lib/resource_policy/policy/policy_configuration.rb', line 20

def initialize(parent_configuration: EMPTY_CONFIGURATION, extra_group_conditions: [])
  @actions = {}
  @attributes = {}
  @parent_configuration = parent_configuration
  @extra_group_conditions = extra_group_conditions
end

Instance Method Details

#action(attribute_name) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/resource_policy/policy/policy_configuration.rb', line 53

def action(attribute_name)
  symbolized_name = attribute_name.to_sym

  @actions[symbolized_name] ||= ActionPolicyConfiguration.new(
    symbolized_name, policy_configuration: self
  )
end

#actionsObject



49
50
51
# File 'lib/resource_policy/policy/policy_configuration.rb', line 49

def actions
  @actions.select { |_name, action| action.configured? }
end

#attribute(attribute_name) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/resource_policy/policy/policy_configuration.rb', line 37

def attribute(attribute_name)
  symbolized_name = attribute_name.to_sym

  @attributes[symbolized_name] ||= AttributesPolicy::AttributeConfiguration.new(
    symbolized_name, policy_configuration: self
  )
end

#attributesObject



45
46
47
# File 'lib/resource_policy/policy/policy_configuration.rb', line 45

def attributes
  @attributes.select { |_name, action| action.configured? }
end

#group(*new_conditions) {|group_config| ... } ⇒ Object

Yields:

  • (group_config)


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/resource_policy/policy/policy_configuration.rb', line 61

def group(*new_conditions)
  return @group if new_conditions.empty?

  unique_new_conditions = new_conditions.map(&:to_sym).uniq
  group_config = self.class.new(
    parent_configuration: self,
    extra_group_conditions: unique_new_conditions
  )

  yield(group_config)
  tap { |config| config.merge(group_config) }
end

#group_conditionsObject



74
75
76
# File 'lib/resource_policy/policy/policy_configuration.rb', line 74

def group_conditions
  (parent_configuration.group_conditions + extra_group_conditions).uniq
end

#initialize_copy(_other) ⇒ Object



27
28
29
30
# File 'lib/resource_policy/policy/policy_configuration.rb', line 27

def initialize_copy(_other)
  @actions = @actions.dup.each.with_object({}) { |(key, value), result| result[key] = value.dup }
  @attributes = @attributes.dup.each.with_object({}) { |(key, value), result| result[key] = value.dup }
end

#merge(other) ⇒ Object



78
79
80
# File 'lib/resource_policy/policy/policy_configuration.rb', line 78

def merge(other)
  MergePolicies.call(self, other)
end

#policy_target(policy_target_name = nil) ⇒ Object



32
33
34
35
# File 'lib/resource_policy/policy/policy_configuration.rb', line 32

def policy_target(policy_target_name = nil)
  @policy_target = policy_target_name if policy_target_name
  @policy_target
end