lib/checkpoint/resource.rb in checkpoint-1.0.3 vs lib/checkpoint/resource.rb in checkpoint-1.1.0

- old
+ new

@@ -33,47 +33,45 @@ # resolution to accommodate whatever fixed, dynamic, or inherited modeling is # most appropriate for the credentials and resources of an application. class Resource attr_reader :entity - # Special string to be used when permitting or searching for permits on all + # Special string to be used when granting or searching for grants on all # types or all resources ALL = '(all)' # Creates a Resource for this entity. Prefer the factory method {::from}, # which applies default conversion rules. This constructor does not # consider whether the entity can covert itself with #to_resource. def initialize(entity) @entity = entity end - # Default conversion from an entity to a Resource. Prefer this to creating - # new instances by hand. - # - # If the entity implements #to_resource, we will delegate to it. Otherwise, - # we will return a Resource for this entity. - def self.from(entity) - if entity.respond_to?(:to_resource) - entity.to_resource - else - new(entity) - end - end - # Covenience factory method to get a Resource that will match all entities # of any type. # # @return [AllOfAnyType] a wildcard resource instance def self.all AllOfAnyType.new end + # Convert this object to a Resource. + # + # For Checkpoint-supplied Resources, this is an identity operation, + # but it allows consistent handling of the built-in types and + # application-supplied types that will either implement this interface or + # convert themselves to a built-in type. This removes the requirement to + # extend Checkpoint types or bind to a specific conversion method. + def to_resource + self + end + # Get the resource type. # # Note that this is not necessarily a class/model type name. It can be # whatever type name is most useful for building tokens and inspecting - # permits for this types. For example, there may be objects that have + # grants for this types. For example, there may be objects that have # subtypes that are not modeled as objects, decorators, or collection # objects (like a specialized type for the root of a tree) that should # be treated as the element type. # # If the entity implements `#resource_type`, we will use that. Otherwise, @@ -125,14 +123,13 @@ # determined by comparing them with `#eql?`. def eql?(other) other.is_a?(Resource) && entity.eql?(other.entity) end - # Check whether two Resources refer to the same entity. + # Check whether two Resources refer to the same entity by type and id. # @param other [Resource] Another Resource to compare with - # @return [Boolean] true when the other Resource's entity is the same as - # determined by comparing them with `#==`. + # @return [Boolean] true when the other Resource's type and id are equal. def ==(other) - other.is_a?(Resource) && entity == other.entity + other.is_a?(Resource) && type == other.type && id == other.id end end end