Class: ActiveLucene::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/active_lucene/document.rb

Constant Summary collapse

PER_PAGE =
20

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Document

Returns a new instance of Document.



7
8
9
10
11
# File 'lib/active_lucene/document.rb', line 7

def initialize(attributes = {})
  @attributes = attributes.stringify_keys
  @highlight = @attributes.delete('highlight')
  @id = @attributes.delete('id') || object_id.to_s
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object (private)



72
73
74
# File 'lib/active_lucene/document.rb', line 72

def method_missing(method_name, *args)
  @attributes[method_name.to_s]
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/active_lucene/document.rb', line 5

def attributes
  @attributes
end

#highlightObject (readonly)

Returns the value of attribute highlight.



5
6
7
# File 'lib/active_lucene/document.rb', line 5

def highlight
  @highlight
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/active_lucene/document.rb', line 5

def id
  @id
end

Class Method Details

.all(opts = {}) ⇒ Object



40
41
42
# File 'lib/active_lucene/document.rb', line 40

def self.all(opts = {})
  find :all, opts
end

.columnsObject



62
63
64
# File 'lib/active_lucene/document.rb', line 62

def self.columns
  []
end

.create!(attributes = {}) ⇒ Object



44
45
46
47
48
# File 'lib/active_lucene/document.rb', line 44

def self.create!(attributes = {})
  returning new(attributes) do |model|
    model.save
  end
end

.find(param, opts = {}) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/active_lucene/document.rb', line 50

def self.find(param, opts = {})
  if param.instance_of? Symbol
    search :all, opts
  else
    search(:id => param).first
  end
end

.paginate(opts) ⇒ Object



66
67
68
# File 'lib/active_lucene/document.rb', line 66

def self.paginate(opts)
  all opts
end

.search(param, opts = {}) ⇒ Object



58
59
60
# File 'lib/active_lucene/document.rb', line 58

def self.search(param, opts = {})
  Index::Searcher.search(param, opts)
end

Instance Method Details

#destroyObject



26
27
28
# File 'lib/active_lucene/document.rb', line 26

def destroy
  Index::Writer.delete @id
end

#saveObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/active_lucene/document.rb', line 13

def save
  document = org.apache.lucene.document.Document.new
  _all = []
  @attributes.each do |key, value|
    document.add Field.new key, value, Field::Store::YES, Field::Index::ANALYZED
    _all << value
  end
  document.add Field.new ID, @id, Field::Store::YES, Field::Index::NOT_ANALYZED
  document.add Field.new ALL, _all.join(' '), Field::Store::NO, Field::Index::ANALYZED
  document.add Field.new TYPE, self.class.to_s, Field::Store::YES, Field::Index::NOT_ANALYZED
  Index::Writer.write document
end

#to_paramObject



36
37
38
# File 'lib/active_lucene/document.rb', line 36

def to_param
  @id
end

#update_attributes(new_attributes) ⇒ Object Also known as: update_attributes!



30
31
32
33
# File 'lib/active_lucene/document.rb', line 30

def update_attributes(new_attributes)
  destroy
  self.class.create! attributes.merge(new_attributes.merge(:id => @id))
end