Class: ToughGuy::MappedQuery

Inherits:
Query
  • Object
show all
Defined in:
lib/toughguy/mapped_query.rb

Instance Attribute Summary

Attributes inherited from Query

#klass

Instance Method Summary collapse

Methods inherited from Query

#avg, #explain, #group, #group_and_avg, #group_and_count, #insert_rate, #map, #map_id, #max, #method_missing, #min, #orig_all, #orig_find_one, #orig_sort, #paginate, #raw, #select, #set_pagination_info, #sort, #update_values

Constructor Details

#initialize(klass, opts = {}) ⇒ MappedQuery

Returns a new instance of MappedQuery.



3
4
5
6
7
8
# File 'lib/toughguy/mapped_query.rb', line 3

def initialize(klass, opts = {})
  super(klass, opts)
  @map = klass.map
  @map_key = klass.map_key
  @map_key_s = @map_key.to_s
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ToughGuy::Query

Instance Method Details

#all(opts = {}) ⇒ Object Also known as: to_a



10
11
12
13
14
# File 'lib/toughguy/mapped_query.rb', line 10

def all(opts={})
  a = []
  each {|o| a << o}
  a
end

#each(opts = {}, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/toughguy/mapped_query.rb', line 18

def each(opts={}, &block)
  find_each(opts).each do |d|
    if o = @map[d[@map_key_s]]
      block.call(o)
    else
      block.call(@klass.load(d))
    end
  end
  self
end

#find_one(opts = {}) ⇒ Object Also known as: first



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

def find_one(opts={})
  if (d = orig_find_one(opts))
    @map[d[@map_key_s]] || @klass.load(d)
  else
    nil
  end
end