Class: ActsAsFerret::FerretResult

Inherits:
BlankSlate
  • Object
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

Methods inherited from BlankSlate

hide

Constructor Details

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

Returns a new instance of FerretResult.



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

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



29
30
31
32
33
34
35
# File 'lib/acts_as_ferret/ferret_result.rb', line 29

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



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

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

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to?(name)
  [ :ferret_score, :ferret_rank,
    :inspect, :method_missing, :respond_to?, :to_record, :to_param, :id
  ].include?(name) ||
    @data.has_key?(name.to_sym) || to_record.respond_to?(name)
end

#to_paramObject



55
56
57
# File 'lib/acts_as_ferret/ferret_result.rb', line 55

def to_param
  return @id
end

#to_recordObject



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

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