Sha256: 9df722efbda8fc2ac9f3a262cced37d5b75ac26d624842c86765d015a42a2f67

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

Stored size: 1.77 KB

Contents

#
# The ActiveFacts Runtime API Instance extension module.
# Copyright (c) 2008 Clifford Heath. Read the LICENSE file.
#
# Instance methods are extended into all instances, whether of value or entity types.
#
module ActiveFacts
  module API
    # Every Instance of a Concept (A Value type or an Entity type) includes the methods of this module:
    module Instance
      # What constellation does this Instance belong to (if any):
      attr_accessor :constellation

      def initialize(args = []) #:nodoc:
        unless (self.class.respond_to?(:identifying_roles))
        #if (self.class.superclass != Object)
          # puts "constructing #{self.class.superclass} with #{args.inspect}"
          super(*args)
        end
      end

      # Verbalise this instance
      def verbalise
        # This method should always be overridden in subclasses
        raise "#{self.class} Instance verbalisation needed"
      end

      # De-assign all functional roles and remove from constellation, if any.
      def delete
        # Delete from the constellation first, so it can remember our identifying role values
        @constellation.delete(self) if @constellation

        # Now, for all roles (from this class and all supertypes), assign nil to all functional roles
        # The counterpart roles get cleared automatically.
        ([self.class]+self.class.supertypes_transitive).each do |klass|
          klass.roles.each do |role_name, role|
            next if role.unary?
            next if !role.unique
            send "#{role.name}=", nil
          end
        end
      end

      module ClassMethods #:nodoc:
        include Concept
        # Add Instance class methods here
      end

      def Instance.included other #:nodoc:
        other.send :extend, ClassMethods
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activefacts-0.6.0 lib/activefacts/api/instance.rb
activefacts-0.7.0 lib/activefacts/api/instance.rb