Class: MarkMapper::Plugins::Querying::DecoratedMarkMapperQuery

Inherits:
Query
  • Object
show all
Includes:
DynamicQuerying::ClassMethods
Defined in:
lib/mark_mapper/plugins/querying/decorated_markmapper_query.rb

Constant Summary

Constants inherited from Query

Query::OptionKeys

Instance Attribute Summary

Attributes inherited from Query

#collection, #criteria, #options

Instance Method Summary collapse

Methods included from DynamicQuerying::ClassMethods

#dynamic_find

Methods inherited from Query

#[], #[]=, #amend, #cursor, #explain, #initialize, #initialize_copy, #inspect, #merge, #object_ids, #to_hash, #update

Methods included from Query::DSL

#all, #count, #empty?, #exists?, #fields, #find, #find_each, #find_one, #ignore, #last, #limit, #only, #paginate, #per_page, #remove, #reverse, #skip, #sort, #where

Constructor Details

This class inherits a constructor from MarkMapper::Query

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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


63
64
65
66
67
68
69
70
71
# File 'lib/mark_mapper/plugins/querying/decorated_markmapper_query.rb', line 63

def method_missing(method, *args, &block)
  return super unless model.respond_to?(method)
  result = model.send(method, *args, &block)
  if result.is_a?(MarkMapper::Query)
    merge(result)
  else
    result
  end
end

Instance Method Details

#criteria_hashObject


32
33
34
# File 'lib/mark_mapper/plugins/querying/decorated_markmapper_query.rb', line 32

def criteria_hash
  @model.dealias_keys super
end

#delete(*ids) ⇒ Object


10
11
12
# File 'lib/mark_mapper/plugins/querying/decorated_markmapper_query.rb', line 10

def delete(*ids)
  where(:_id => ids.flatten).remove
end

#delete_all(options = {}) ⇒ Object


14
15
16
# File 'lib/mark_mapper/plugins/querying/decorated_markmapper_query.rb', line 14

def delete_all(options = {})
  where(options).remove
end

#destroy(*ids) ⇒ Object


18
19
20
# File 'lib/mark_mapper/plugins/querying/decorated_markmapper_query.rb', line 18

def destroy(*ids)
  [find!(*ids.flatten.compact.uniq)].flatten.each { |doc| doc.destroy }
end

#destroy_all(options = {}) ⇒ Object


22
23
24
# File 'lib/mark_mapper/plugins/querying/decorated_markmapper_query.rb', line 22

def destroy_all(options={})
  find_each(options) { |document| document.destroy }
end

#find!(*ids) ⇒ Object

Raises:


50
51
52
53
54
55
56
57
58
59
# File 'lib/mark_mapper/plugins/querying/decorated_markmapper_query.rb', line 50

def find!(*ids)
  ids = Array(ids).flatten.uniq
  raise DocumentNotFound, "Couldn't find without an ID" if ids.size == 0

  find(*ids).tap do |result|
    if result.nil? || ids.size != Array(result).size
      raise DocumentNotFound, "Couldn't find all of the ids (#{ids.join(',')}). Found #{Array(result).size}, but was expecting #{ids.size}"
    end
  end
end

#model(model = nil) ⇒ Object


26
27
28
29
30
# File 'lib/mark_mapper/plugins/querying/decorated_markmapper_query.rb', line 26

def model(model=nil)
  return @model if model.nil?
  @model = model
  self
end

#options_hashObject


36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mark_mapper/plugins/querying/decorated_markmapper_query.rb', line 36

def options_hash
  super.tap do |options|
    case options[:fields]
    when Hash
      options[:fields] = @model.dealias options[:fields]
    when Array
      options[:fields] = options[:fields].map do |field|
        key = keys[field.to_s]
        key && key.abbr || field
      end
    end
  end
end