Class: Findex::DocumentDecorator

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/findex/document_decorator.rb

Overview

A decorator class to be used around Xapian::Documents

Constant Summary collapse

TIME_FORMAT =
'%s'.freeze
DATE_FORMAT =
'%Y%m%d'.freeze
VALUE_SLOTS =
{
  path: 0,
  mtime: 1,
  date: 2
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xapian_document, root_path, file = nil) ⇒ DocumentDecorator

Returns a new instance of DocumentDecorator.



20
21
22
23
24
25
# File 'lib/findex/document_decorator.rb', line 20

def initialize(xapian_document, root_path, file = nil)
  @xapian_document = xapian_document
  @root_path = root_path
  super(xapian_document)
  update_values_from(file) if file
end

Instance Attribute Details

#xapian_documentObject (readonly)

Returns the value of attribute xapian_document.



18
19
20
# File 'lib/findex/document_decorator.rb', line 18

def xapian_document
  @xapian_document
end

Instance Method Details

#actual_mtimeObject



40
41
42
# File 'lib/findex/document_decorator.rb', line 40

def actual_mtime
  Time.at(full_path.mtime.to_i)
end

#add_value(slot, value) ⇒ Object



79
80
81
82
# File 'lib/findex/document_decorator.rb', line 79

def add_value(slot, value)
  return super if slot.is_a?(Integer)
  super(VALUE_SLOTS[slot], value)
end

#changed?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/findex/document_decorator.rb', line 66

def changed?
  actual_mtime > mtime
end

#dateObject



49
50
51
# File 'lib/findex/document_decorator.rb', line 49

def date
  @date ||= Date.strptime(value(:date), DATE_FORMAT)
end

#date=(date) ⇒ Object



53
54
55
56
# File 'lib/findex/document_decorator.rb', line 53

def date=(date)
  add_value(:date, date.strftime(DATE_FORMAT))
  @date = date
end

#deleted?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/findex/document_decorator.rb', line 62

def deleted?
  !exists?
end

#exists?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/findex/document_decorator.rb', line 58

def exists?
  full_path.exist?
end

#extensionObject



70
71
72
# File 'lib/findex/document_decorator.rb', line 70

def extension
  path.extname[1..-1]
end

#full_pathObject



94
95
96
# File 'lib/findex/document_decorator.rb', line 94

def full_path
  @root_path + path
end

#insert(db, term_generator) ⇒ Object



89
90
91
92
# File 'lib/findex/document_decorator.rb', line 89

def insert(db, term_generator)
  index(term_generator)
  db.add_document(xapian_document)
end

#mtimeObject



36
37
38
# File 'lib/findex/document_decorator.rb', line 36

def mtime
  @mtime ||= Time.strptime(value(:mtime), TIME_FORMAT)
end

#mtime=(time) ⇒ Object



44
45
46
47
# File 'lib/findex/document_decorator.rb', line 44

def mtime=(time)
  add_value(:mtime, time.strftime(TIME_FORMAT))
  @mtime = time
end

#pathObject



27
28
29
# File 'lib/findex/document_decorator.rb', line 27

def path
  @path ||= value(:path)
end

#path=(path) ⇒ Object



31
32
33
34
# File 'lib/findex/document_decorator.rb', line 31

def path=(path)
  add_value(:path, path.to_s)
  @path = path
end

#update(db, term_generator) ⇒ Object



84
85
86
87
# File 'lib/findex/document_decorator.rb', line 84

def update(db, term_generator)
  index(term_generator)
  db.replace_document(docid, xapian_document)
end

#value(slot) ⇒ Object



74
75
76
77
# File 'lib/findex/document_decorator.rb', line 74

def value(slot)
  return super if slot.is_a?(Integer)
  super(VALUE_SLOTS[slot])
end