Class: ActsAsFerret::FerretResult

Inherits:
BlankSlate show all
Includes:
ResultAttributes
Defined in:
lib/ferret_result.rb

Instance Attribute Summary collapse

Attributes included from ResultAttributes

#ferret_rank, #ferret_score

Instance Method Summary collapse

Constructor Details

#initialize(model, id, score, rank, data = {}) ⇒ FerretResult

Returns a new instance of FerretResult.



17
18
19
20
21
22
23
24
# File 'lib/ferret_result.rb', line 17

def initialize(model, id, score, rank, data = {})
  @model = model.constantize
  @id = id
  @ferret_score = score
  @ferret_rank  = rank
  @data = data
  @use_record = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/ferret_result.rb', line 30

def method_missing(method, *args, &block)
  if [:highlight, :document_number, :query_for_record].include?(method.to_sym)
    @model.send method, id, *args
  elsif (@ar_record && @use_record) || !@data.has_key?(method)
    to_record.send method, *args, &block
  else
    @data[method]
  end
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



14
15
16
# File 'lib/ferret_result.rb', line 14

def id
  @id
end

Instance Method Details

#[](attr) ⇒ Object

An implementation of rm.jkraemer.net/issues/show/161



41
# File 'lib/ferret_result.rb', line 41

def [](attr) method_missing(attr) end

#inspectObject



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

def inspect
  "#<FerretResult wrapper for #{@model} with id #{@id}"
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/ferret_result.rb', line 43

def respond_to?(name)
  methods.include?(name.to_s) || @data.has_key?(name.to_sym) || to_record.respond_to?(name)
end

#to_recordObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/ferret_result.rb', line 47

def to_record
  unless @ar_record
    @ar_record = @model.find(id)
    @ar_record.ferret_rank  = ferret_rank
    @ar_record.ferret_score = ferret_score
    # don't try to fetch attributes from RDig based records
    @use_record = !@ar_record.class.included_modules.include?(ActsAsFerret::RdigAdapter)
  end
  @ar_record
end