#194
[acts_as_ferret.git] / lib / ferret_result.rb
blobf15cb0430d32b1c52d5db79a28e3c660cc7caa8c
1 module ActsAsFerret
3   # mixed into the FerretResult and AR classes calling acts_as_ferret
4   module ResultAttributes
5     # holds the score this record had when it was found via
6     # acts_as_ferret
7     attr_accessor :ferret_score
9     attr_accessor :ferret_rank
10   end
12   class FerretResult < BlankSlate
13     include ResultAttributes
14     attr_accessor :id
15     reveal :methods
17     def initialize(model, id, score, rank, data = {})
18       @model = model.constantize
19       @id = id
20       @ferret_score = score
21       @ferret_rank  = rank
22       @data = data
23     end
24     
25     def method_missing(method, *args, &block)
26       if @ar_record || !@data.has_key?(method)
27         to_record.send method, *args, &block
28       else
29         @data[method]
30       end
31     end
33     def respond_to?(name)
34       methods.include?(name.to_s) || @data.has_key?(name.to_sym) || to_record.respond_to?(name)
35     end
37     def to_record
38       unless @ar_record
39         @ar_record = @model.find(id)
40         @ar_record.ferret_rank  = ferret_rank
41         @ar_record.ferret_score = ferret_score
42       end
43       @ar_record
44     end
45   end
46 end