Class: ActsAsFerret::FerretResult

Inherits:
BlankSlate show all
Includes:
ResultAttributes
Defined in:
lib/acts_as_ferret/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/acts_as_ferret/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
# File 'lib/acts_as_ferret/ferret_result.rb', line 30

def method_missing(method, *args, &block)
  if (@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/acts_as_ferret/ferret_result.rb', line 14

def id
  @id
end

Instance Method Details

#inspectObject



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

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

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/acts_as_ferret/ferret_result.rb', line 38

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

#to_recordObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/acts_as_ferret/ferret_result.rb', line 42

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