Sha256: 8e68307ab5cce1aeffbc0c638f5aa0b41c17ea9d90dc0511a23b115a69a3041f

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

require "rdf/isomorphic"

module Ddr
  module Auth
    #
    # The agent (person or group) to whom a role is granted.
    # Use subclasses for concrete instances.
    #
    # @abstract
    #
    class Agent < ActiveTriples::Resource
      
      include Hydra::Validations
      include RDF::Isomorphic

      configure type: RDF::FOAF.Agent
      property :name, predicate: RDF::FOAF.name

      validates_presence_of :name

      # Agents are considered equal if their names (and types) are equal
      def ==(other)
        isomorphic_with? other
      end

      def to_s
        agent_name
      end

      def to_h
        {type: agent_type, name: agent_name}
      end

      def inspect
        "#<#{self.class.name}(#{self})>"
      end

      def agent_name
        name.first
      end

      def agent_type
        self.class.agent_type
      end

      class << self
        # Factory method
        #   Assumes that the string representation of the object may be used as the agent's name.
        # @param obj [Object] the object from which to build the agent
        # @return [Agent] the agent
        def build(obj)
          new.tap do |agent| 
            agent.name = obj.to_s
            if agent.invalid?
              raise Ddr::Models::Error, "Invalid #{self.name}: #{agent.errors.messages.inspect}"
            end
          end
        end

        def agent_type
          @agent_type ||= self.name.split("::").last.underscore.to_sym
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ddr-models-1.13.1 lib/ddr/auth/agent.rb
ddr-models-1.13.0 lib/ddr/auth/agent.rb