Class: Caster::Reference

Inherits:
Object
  • Object
show all
Defined in:
lib/caster/reference.rb

Instance Method Summary collapse

Constructor Details

#initialize(docs) ⇒ Reference

Returns a new instance of Reference.



11
12
13
14
15
# File 'lib/caster/reference.rb', line 11

def initialize docs
  @docs = docs
  @method_chain = []
  @accessor = Accessor.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



22
23
24
25
# File 'lib/caster/reference.rb', line 22

def method_missing method_name, *args
  @method_chain << [method_name, args]
  self
end

Instance Method Details

#evaluate(target_doc) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/caster/reference.rb', line 27

def evaluate target_doc
  @docs.each do |doc|
    if @predicate.call doc
      value = @accessor.get doc, @value_field
      @method_chain.each do |args|
        value = value.send args[0], *args[1]
      end
      return value
    end
  end
  nil
end

#where(&predicate) ⇒ Object



17
18
19
20
# File 'lib/caster/reference.rb', line 17

def where &predicate
  @predicate = predicate
  self
end