Class: PM::PolicyElement

Inherits:
Object
  • Object
show all
Defined in:
lib/policy_machine/policy_element.rb

Overview

A generic policy element in a policy machine. A policy element can be a user, user attribute, object, object attribute or operation set. This is an abstract base class and should not itself be instantiated.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unique_identifier, policy_machine_uuid, pm_storage_adapter, stored_pe = nil, extra_attributes = {}) ⇒ PolicyElement

Create a new policy element with the given name and type.



15
16
17
18
19
20
21
22
# File 'lib/policy_machine/policy_element.rb', line 15

def initialize(unique_identifier, policy_machine_uuid, pm_storage_adapter, stored_pe = nil, extra_attributes = {})
  @unique_identifier = unique_identifier.to_s
  @policy_machine_uuid = policy_machine_uuid.to_s
  @pm_storage_adapter = pm_storage_adapter
  @stored_pe = stored_pe
  @extra_attributes = extra_attributes
  methodize_extra_attributes!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

Delegate extra attribute reads to stored_pe



97
98
99
100
101
102
103
# File 'lib/policy_machine/policy_element.rb', line 97

def method_missing(meth, *args)
  if args.none? && stored_pe.respond_to?(meth)
    stored_pe.send(meth)
  else
    super
  end
end

Instance Attribute Details

#extra_attributesObject

Returns the value of attribute extra_attributes.



11
12
13
# File 'lib/policy_machine/policy_element.rb', line 11

def extra_attributes
  @extra_attributes
end

#policy_machine_uuidObject

Returns the value of attribute policy_machine_uuid.



9
10
11
# File 'lib/policy_machine/policy_element.rb', line 9

def policy_machine_uuid
  @policy_machine_uuid
end

#stored_peObject

Returns the value of attribute stored_pe.



10
11
12
# File 'lib/policy_machine/policy_element.rb', line 10

def stored_pe
  @stored_pe
end

#unique_identifierObject

Returns the value of attribute unique_identifier.



8
9
10
# File 'lib/policy_machine/policy_element.rb', line 8

def unique_identifier
  @unique_identifier
end

Class Method Details

.convert_stored_pe_to_pe(stored_pe, pm_storage_adapter, pe_class) ⇒ Object

Convert a stored_pe to an instantiated pe



76
77
78
79
80
81
82
83
# File 'lib/policy_machine/policy_element.rb', line 76

def self.convert_stored_pe_to_pe(stored_pe, pm_storage_adapter, pe_class)
  pe_class.new(
    stored_pe.unique_identifier,
    stored_pe.policy_machine_uuid,
    pm_storage_adapter,
    stored_pe
  )
end

Instance Method Details

#==(other_pe) ⇒ Object

Returns true if self is identical to other and false otherwise.



88
89
90
91
92
# File 'lib/policy_machine/policy_element.rb', line 88

def ==(other_pe)
  self.class == other_pe.class &&
  self.unique_identifier == other_pe.unique_identifier &&
  self.policy_machine_uuid == other_pe.policy_machine_uuid
end

#assign_to(dst_policy_element) ⇒ Object

Assign self to destination policy element This method is sensitive to the type of self and dst_policy_element



34
35
36
37
38
39
# File 'lib/policy_machine/policy_element.rb', line 34

def assign_to(dst_policy_element)
  unless allowed_assignee_classes.any?{|aac| dst_policy_element.is_a?(aac)}
    raise(ArgumentError, "expected dst_policy_element to be one of #{allowed_assignee_classes.to_s}; got #{dst_policy_element.class} instead.")
  end
  @pm_storage_adapter.assign(self.stored_pe, dst_policy_element.stored_pe)
end

#connected?(other_pe) ⇒ Boolean

Determine if self is connected to other node

Returns:

  • (Boolean)


26
27
28
# File 'lib/policy_machine/policy_element.rb', line 26

def connected?(other_pe)
  @pm_storage_adapter.connected?(self.stored_pe, other_pe.stored_pe)
end

#deleteObject

Remove self, and any assignments to or from self. Does not remove any other elements. Returns true if persisted object was successfully removed.



53
54
55
56
57
58
59
# File 'lib/policy_machine/policy_element.rb', line 53

def delete
  if self.stored_pe && self.stored_pe.persisted
    @pm_storage_adapter.delete(stored_pe)
    self.stored_pe = nil
    true
  end
end

#inspectObject



109
110
111
# File 'lib/policy_machine/policy_element.rb', line 109

def inspect
  "#<#{self.class} #{unique_identifier}>"
end

#respond_to_missing?(meth, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/policy_machine/policy_element.rb', line 105

def respond_to_missing?(meth, include_private = false)
  stored_pe.respond_to?(meth, include_private) || super
end

#unassign(dst_policy_element) ⇒ Object

Remove assignment from self to destination policy element Returns boolean indicating whether assignment was successfully removed.



45
46
47
# File 'lib/policy_machine/policy_element.rb', line 45

def unassign(dst_policy_element)
  @pm_storage_adapter.unassign(self.stored_pe, dst_policy_element.stored_pe)
end

#update(attr_hash) ⇒ Object

Updates extra attributes with the passed-in values. Will not remove other attributes not in the hash. Returns true if no errors occurred.



65
66
67
68
69
70
71
72
# File 'lib/policy_machine/policy_element.rb', line 65

def update(attr_hash)
  @extra_attributes.merge!(attr_hash)
  methodize_extra_attributes!
  if self.stored_pe && self.stored_pe.persisted
    @pm_storage_adapter.update(self.stored_pe, attr_hash)
    true
  end
end