Sha256: a272b53f3516946f4ba1e106078c350fb59f7885b2b72d2f889af93bc53569d2

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

#
#       ActiveFacts Runtime API
#       InstanceIndex class
#
# Copyright (c) 2009 Clifford Heath. Read the LICENSE file.
#
module ActiveFacts
  module API
    #
    # Each Constellation maintains an InstanceIndex for each Concept in its Vocabulary.
    # The InstanceIndex object is returned when you call @constellation.Concept with no
    # arguments (where Concept is the concept name you're interested in)
    #
    class InstanceIndex
      def []=(key, value)   #:nodoc:
        raise "Adding RoleProxy to InstanceIndex" if value && RoleProxy === value
        h[key] = value
      end

      def [](*args)
        a = args
        #a = naked(args)
#        p "vvvv",
#          args,
#          a,
#          keys.map{|k| v=super(k); (RoleProxy === k ? "*" : "")+k.to_s+"=>"+(RoleProxy === v ? "*" : "")+v.to_s}*",",
#          "^^^^"
        h[*a]
        #super(*a)
      end

      def size
        h.size
      end

      def each &b
        h.each &b
      end

      def map &b
        h.map &b
      end

      def detect &b
        r = h.detect &b
        r ? r[1] : nil
      end

      # Return an array of all the instances of this concept
      def values
        h.values
      end

      # Return an array of the identifying role values arrays for all the instances of this concept
      def keys
        h.keys
      end

      def delete_if(&b)   #:nodoc:
        h.delete_if &b
      end

    private
      def h
        @hash ||= {}
      end

      def naked(o)
        case o
        when Array
          o.map{|e| naked(e) }
        when RoleProxy
          o.__getobj__
        else
          o
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activefacts-0.8.9 lib/activefacts/api/instance_index.rb
activefacts-0.8.8 lib/activefacts/api/instance_index.rb