class Ferret::Index::LazyDoc

Summary

When a document is retrieved from the index a LazyDoc is returned. Actually, LazyDoc is just a modified Hash object which lazily adds fields to itself when they are accessed. You should not that they keys method will return nothing until you actually access one of the fields. To see what fields are available use #fields rather than LazyDoc#keys. To load all fields use the #load method.

Example

doc = index_reader[0]

doc.keys     #=> []
doc.values   #=> []
doc.fields   #=> [:title, :content]

title = doc[:title] #=> "the title"
doc.keys     #=> [:title]
doc.values   #=> ["the title"]
doc.fields   #=> [:title, :content]

doc.load
doc.keys     #=> [:title, :content]
doc.values   #=> ["the title", "the content"]
doc.fields   #=> [:title, :content]

Public Instance Methods

default(key) → string click to toggle source

This method is used internally to lazily load fields. You should never really need to call it yourself.

static VALUE
frb_lzd_default(VALUE self, VALUE rkey)
{
    LazyDoc *lazy_doc = (LazyDoc *)DATA_PTR(rb_ivar_get(self, id_data));
    Symbol field = frb_field(rkey);
    VALUE rfield = FSYM2SYM(field);

    return frb_lazy_df_load(self, rfield, lazy_doc_get(lazy_doc, field));
}
fields → array of available fields click to toggle source

Returns the list of fields stored for this particular document. If you try to access any of these fields in the document the field will be loaded. Try to access any other field an nil will be returned.

static VALUE
frb_lzd_fields(VALUE self)
{
    return rb_ivar_get(self, id_fields);
}
load → lazy_doc click to toggle source

Load all unloaded fields in the document from the index.

static VALUE
frb_lzd_load(VALUE self)
{
    LazyDoc *lazy_doc = (LazyDoc *)DATA_PTR(rb_ivar_get(self, id_data));
    int i;
    for (i = 0; i < lazy_doc->size; i++) {
        LazyDocField *lazy_df = lazy_doc->fields[i];
        frb_lazy_df_load(self, FSYM2SYM(lazy_df->name), lazy_df);
    }
    return self;
}