Class: Daodalus::DSL::Update

Inherits:
Object
  • Object
show all
Includes:
Clause, Queries, Updates
Defined in:
lib/daodalus/dsl/update.rb

Instance Method Summary collapse

Methods included from Updates

#find_and_modify, #update, #upsert

Methods included from Queries

#find, #find_one, #select, #where

Methods included from Clause

#to_projection, #to_query, #to_update

Constructor Details

#initialize(dao, query) ⇒ Update

Returns a new instance of Update.



8
9
10
11
# File 'lib/daodalus/dsl/update.rb', line 8

def initialize(dao, query)
  @dao    = dao
  @query  = query
end

Instance Method Details

#add_each_to_set(field, values) ⇒ Object



53
54
55
# File 'lib/daodalus/dsl/update.rb', line 53

def add_each_to_set(field, values)
  with_clause '$addToSet' => { field => { '$each' => values } }
end

#add_to_set(field, values) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/daodalus/dsl/update.rb', line 45

def add_to_set(field, values)
  if values.length == 1
    with_clause '$addToSet' => { field => values.first }
  else
    add_each_to_set field, values
  end
end

#dec(field, amount) ⇒ Object



25
26
27
# File 'lib/daodalus/dsl/update.rb', line 25

def dec(field, amount)
  with_clause '$inc' => { field => -amount }
end

#inc(field, amount) ⇒ Object



21
22
23
# File 'lib/daodalus/dsl/update.rb', line 21

def inc(field, amount)
  with_clause '$inc' => { field => amount }
end

#pop_first(field) ⇒ Object



57
58
59
# File 'lib/daodalus/dsl/update.rb', line 57

def pop_first(field)
  with_clause '$pop' => { field => -1 }
end

#pop_last(field) ⇒ Object



61
62
63
# File 'lib/daodalus/dsl/update.rb', line 61

def pop_last(field)
  with_clause '$pop' => { field => 1 }
end

#pull(field, values) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/daodalus/dsl/update.rb', line 65

def pull(field, values)
  if values.length == 1
    with_clause '$pull' => { field => values.first }
  else
    pull_all field, values
  end
end

#pull_all(field, values) ⇒ Object



73
74
75
# File 'lib/daodalus/dsl/update.rb', line 73

def pull_all(field, values)
  with_clause '$pullAll' => { field => values }
end

#push(field, values) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/daodalus/dsl/update.rb', line 33

def push(field, values)
  if values.length == 1
    with_clause '$push' => { field => values.first }
  else
    push_all field, values
  end
end

#push_all(field, values) ⇒ Object



41
42
43
# File 'lib/daodalus/dsl/update.rb', line 41

def push_all(field, values)
  with_clause '$pushAll' => { field => values }
end

#rename(field, value) ⇒ Object



29
30
31
# File 'lib/daodalus/dsl/update.rb', line 29

def rename(field, value)
  with_clause '$rename' => { field => value.to_s }
end

#set(fields) ⇒ Object



13
14
15
# File 'lib/daodalus/dsl/update.rb', line 13

def set(fields)
  with_clause '$set' => fields
end

#unset(fields) ⇒ Object



17
18
19
# File 'lib/daodalus/dsl/update.rb', line 17

def unset(fields)
  with_clause '$unset' => fields.reduce({}) { |acc, f| acc.merge(f => 1) }
end