| 1 |
from lunr.exceptions import BaseLunrException |
| 2 |
|
| 3 |
|
| 4 |
class FieldRef: |
| 5 |
JOINER = "/" |
| 6 |
|
| 7 |
def __init__(self, doc_ref, field_name, string_value=None): |
| 8 |
self.doc_ref = doc_ref |
| 9 |
self.field_name = field_name |
| 10 |
self._string_value = string_value |
| 11 |
|
| 12 |
def __repr__(self): |
| 13 |
return '<FieldRef field="{}" ref="{}">'.format(self.field_name, self.doc_ref) |
| 14 |
|
| 15 |
@classmethod |
| 16 |
def from_string(cls, string): |
| 17 |
if cls.JOINER not in string: |
| 18 |
raise BaseLunrException("Malformed field ref string") |
| 19 |
field_ref, doc_ref = string.split(cls.JOINER, 1) |
| 20 |
return cls(doc_ref, field_ref, string) |
| 21 |
|
| 22 |
def __str__(self): |
| 23 |
if self._string_value is None: |
| 24 |
self._string_value = self.field_name + self.JOINER + str(self.doc_ref) |
| 25 |
|
| 26 |
return self._string_value |