Commit cb47ec84 authored by Toby Dickenson's avatar Toby Dickenson

merged toby-subclass-index-branch: Collector 284: KeywordIndex and FieldIndex subclassing

parent 69440268
...@@ -62,12 +62,7 @@ class KeywordIndex(UnIndex,PluggableIndex.PluggableIndex,Persistent, ...@@ -62,12 +62,7 @@ class KeywordIndex(UnIndex,PluggableIndex.PluggableIndex,Persistent,
# attribute we're interested in. If the attribute is callable, # attribute we're interested in. If the attribute is callable,
# we'll do so. # we'll do so.
newKeywords = getattr(obj, self.id, ()) newKeywords = self._get_object_keywords(obj)
if callable(newKeywords):
newKeywords = newKeywords()
if type(newKeywords) is StringType:
newKeywords = (newKeywords, )
oldKeywords = self._unindex.get(documentId, None) oldKeywords = self._unindex.get(documentId, None)
...@@ -96,6 +91,14 @@ class KeywordIndex(UnIndex,PluggableIndex.PluggableIndex,Persistent, ...@@ -96,6 +91,14 @@ class KeywordIndex(UnIndex,PluggableIndex.PluggableIndex,Persistent,
self.insertForwardIndexEntry(kw, documentId) self.insertForwardIndexEntry(kw, documentId)
return 1 return 1
def _get_object_keywords(self,obj):
newKeywords = getattr(obj, self.id, ())
if callable(newKeywords):
newKeywords = newKeywords()
if hasattr(newKeywords,'capitalize'): # is it string-like ?
newKeywords = (newKeywords, )
return newKeywords
def unindex_objectKeywords(self, documentId, keywords): def unindex_objectKeywords(self, documentId, keywords):
""" carefully unindex the object with integer id 'documentId'""" """ carefully unindex the object with integer id 'documentId'"""
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
"""Simple column indices""" """Simple column indices"""
__version__='$Revision: 1.7 $'[11:-2] __version__='$Revision: 1.8 $'[11:-2]
from Globals import Persistent from Globals import Persistent
from Acquisition import Implicit from Acquisition import Implicit
...@@ -216,16 +216,8 @@ class UnIndex(Persistent, Implicit): ...@@ -216,16 +216,8 @@ class UnIndex(Persistent, Implicit):
returnStatus = 0 returnStatus = 0
# First we need to see if there's anything interesting to look at # First we need to see if there's anything interesting to look at
# self.id is the name of the index, which is also the name of the datum = self._get_object_datum(obj)
# attribute we're interested in. If the attribute is callable,
# we'll do so.
try:
datum = getattr(obj, self.id)
if callable(datum):
datum = datum()
except AttributeError:
datum = _marker
# We don't want to do anything that we don't have to here, so we'll # We don't want to do anything that we don't have to here, so we'll
# check to see if the new and existing information is the same. # check to see if the new and existing information is the same.
oldDatum = self._unindex.get(documentId, _marker) oldDatum = self._unindex.get(documentId, _marker)
...@@ -241,6 +233,18 @@ class UnIndex(Persistent, Implicit): ...@@ -241,6 +233,18 @@ class UnIndex(Persistent, Implicit):
return returnStatus return returnStatus
def _get_object_datum(self,obj):
# self.id is the name of the index, which is also the name of the
# attribute we're interested in. If the attribute is callable,
# we'll do so.
try:
datum = getattr(obj, self.id)
if callable(datum):
datum = datum()
except AttributeError:
datum = _marker
return datum
def numObjects(self): def numObjects(self):
""" return number of indexed objects """ """ return number of indexed objects """
return len(self._index) return len(self._index)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment