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

extra_attributes[RW]
policy_machine_uuid[RW]
stored_pe[RW]
unique_identifier[RW]

Public Class Methods

convert_stored_pe_to_pe(stored_pe, pm_storage_adapter, pe_class) click to toggle source

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
new(unique_identifier, policy_machine_uuid, pm_storage_adapter, stored_pe = nil, extra_attributes = {}) click to toggle source

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

==(other_pe) click to toggle source

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_to(dst_policy_element) click to toggle source

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
connected?(other_pe) click to toggle source

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
delete() click to toggle source

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
inspect() click to toggle source
# File lib/policy_machine/policy_element.rb, line 109
def inspect
  "#<#{self.class} #{unique_identifier}>"
end
method_missing(meth, *args) click to toggle source

Delegate extra attribute reads to stored_pe

Calls superclass method
# 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
respond_to_missing?(meth, include_private = false) click to toggle source
Calls superclass method
# 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) click to toggle source

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
update(attr_hash) click to toggle source

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

allowed_assignee_classes() click to toggle source
# File lib/policy_machine/policy_element.rb, line 114
def allowed_assignee_classes
  raise "Must override this method in a subclass"
end
methodize_extra_attributes!() click to toggle source

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