class PM::PolicyElement
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.
Attributes
Public Class Methods
Convert a stored_pe
to an instantiated pe
# 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
Create a new policy element with the given name and type.
# 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
Public Instance Methods
Returns true if self is identical to other and false otherwise.
# 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 self to destination policy element This method is sensitive to the type of self and dst_policy_element
# 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
Determine if self is connected to other node
# 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
Remove self, and any assignments to or from self. Does not remove any other elements. Returns true if persisted object was successfully removed.
# 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
# File lib/policy_machine/policy_element.rb, line 109 def inspect "#<#{self.class} #{unique_identifier}>" end
Delegate extra attribute reads to stored_pe
# 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
# 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
Remove assignment from self to destination policy element Returns boolean indicating whether assignment was successfully removed.
# 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
Updates extra attributes with the passed-in values. Will not remove other attributes not in the hash. Returns true if no errors occurred.
# 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
Protected Instance Methods
# File lib/policy_machine/policy_element.rb, line 114 def allowed_assignee_classes raise "Must override this method in a subclass" end
Allow magic attribute methods like in ActiveRecord
# File lib/policy_machine/policy_element.rb, line 121 def methodize_extra_attributes! @extra_attributes.keys.each do |attr| define_singleton_method attr, lambda {@extra_attributes[attr]} unless respond_to?(attr) end end