Module: MarkMapper::Query::DSL

Included in:
MarkMapper::Query
Defined in:
lib/mark_mapper/query.rb

Overview

finder DSL methods to delegate to your model if you're building an ODM e.g. MyModel.last needs to be equivalent to MyModel.query.last

Instance Method Summary collapse

Instance Method Details

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


100
101
102
103
104
# File 'lib/mark_mapper/query.rb', line 100

def all(opts={})
  [].tap do |docs|
    find_each(opts) {|doc| docs << doc }
  end
end

#count(opts = {}) ⇒ Object Also known as: size


115
116
117
118
119
# File 'lib/mark_mapper/query.rb', line 115

def count(opts={})
  query = clone.amend(opts)
  cursor = query.cursor
  cursor.count
end

#empty?Boolean

Returns:


165
166
167
# File 'lib/mark_mapper/query.rb', line 165

def empty?
  count == 0
end

#exists?(query_options = {}) ⇒ Boolean Also known as: exist?

Returns:


169
170
171
# File 'lib/mark_mapper/query.rb', line 169

def exists?(query_options={})
  !only(:_id).find_one(query_options).nil?
end

#fields(*args) ⇒ Object

def distinct(key, opts = {})

query = clone.amend(opts)
query.collection.distinct(key, query.criteria_hash)

end


126
127
128
# File 'lib/mark_mapper/query.rb', line 126

def fields(*args)
  clone.tap { |query| query.options[:fields] = *args }
end

#find(*ids) ⇒ Object


88
89
90
91
92
93
94
95
96
97
98
# File 'lib/mark_mapper/query.rb', line 88

def find(*ids)
  return nil if ids.empty?

  single_id_find = ids.size == 1 && !ids[0].is_a?(Array)

  if single_id_find
    first(:_id => ids[0])
  else
    all(:_id => ids.flatten)
  end
end

#find_each(opts = {}) ⇒ Object Also known as: each


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mark_mapper/query.rb', line 67

def find_each(opts={})
  query = clone.amend(opts)

  if block_given?
    result = nil
    query.cursor do |cursor|
      result = cursor
      cursor.each { |doc| yield doc }
      cursor.rewind!
    end
    result
  else
    query.cursor
  end
end

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


83
84
85
86
# File 'lib/mark_mapper/query.rb', line 83

def find_one(opts={})
  query = clone.amend(opts)
  query.collection.find_one(query.criteria_hash, query.options_hash)
end

#ignore(*args) ⇒ Object


130
131
132
# File 'lib/mark_mapper/query.rb', line 130

def ignore(*args)
  set_field_inclusion(args, 0)
end

#last(opts = {}) ⇒ Object


106
107
108
# File 'lib/mark_mapper/query.rb', line 106

def last(opts={})
  clone.amend(opts).reverse.find_one
end

#limit(count = nil) ⇒ Object


138
139
140
# File 'lib/mark_mapper/query.rb', line 138

def limit(count=nil)
  clone.tap { |query| query.options[:limit] = count }
end

#only(*args) ⇒ Object


134
135
136
# File 'lib/mark_mapper/query.rb', line 134

def only(*args)
  set_field_inclusion(args, 1)
end

#paginate(opts = {}) ⇒ Object


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mark_mapper/query.rb', line 53

def paginate(opts={})
  page      = opts.delete(:page)
  limit     = opts.delete(:per_page) || per_page
  total_entries = opts.delete(:total_entries)
  query     = clone.amend(opts)
  paginator = Pagination::Paginator.new(total_entries || query.count, page, limit)
  docs      = query.amend({
    :limit => paginator.limit,
    :skip  => paginator.skip,
  }).all

  Pagination::Collection.new(docs, paginator)
end

#per_page(limit = nil) ⇒ Object


47
48
49
50
51
# File 'lib/mark_mapper/query.rb', line 47

def per_page(limit=nil)
  return @per_page || MarkLogic::Cursor::DEFAULT_PAGE_LENGTH if limit.nil?
  @per_page = limit
  self
end

#remove(opts = {}) ⇒ Object


110
111
112
113
# File 'lib/mark_mapper/query.rb', line 110

def remove(opts={})
  query = clone.amend(opts)
  query.collection.remove(query.criteria_hash, query.options_hash)
end

#reverseObject


142
143
144
145
146
147
148
149
150
151
# File 'lib/mark_mapper/query.rb', line 142

def reverse
  clone.tap do |query|
    sort = query[:sort]
    if sort.nil?
      query.options[:sort] = [[:_id, -1]]
    else
      query.options[:sort] = sort.map { |s| [s[0], -s[1]] }
    end
  end
end

#skip(count = nil) ⇒ Object Also known as: offset


153
154
155
# File 'lib/mark_mapper/query.rb', line 153

def skip(count=nil)
  clone.tap { |query| query.options[:skip] = count }
end

#sort(*args) ⇒ Object Also known as: order


157
158
159
# File 'lib/mark_mapper/query.rb', line 157

def sort(*args)
  clone.tap { |query| query.options[:sort] = *args }
end

#where(hash = {}) ⇒ Object Also known as: filter


161
162
163
# File 'lib/mark_mapper/query.rb', line 161

def where(hash={})
  clone.tap { |query| query.criteria.merge!(CriteriaHash.new(hash)) }
end