Module: Toy::Mongo::Querying

Extended by:
ActiveSupport::Concern
Defined in:
lib/toy/mongo/querying.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

PluckyMethods =
[
  :where, :filter, :limit, :skip, :offset, :sort, :order,
  :fields, :ignore, :only,
  :each, :find_each,
  :count, :size, :distinct,
  :last, :first, :all, :paginate,
  :exists?, :exist?, :empty?,
  :to_a, :remove,
]

Instance Method Summary collapse

Instance Method Details

#atomic_update(update, opts = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/toy/mongo/querying.rb', line 97

def atomic_update(update, opts={})
  options  = {}
  criteria = {'_id' => id}
  criteria.update(opts[:criteria]) if opts[:criteria]
  options[:safe] = opts.key?(:safe) ? opts[:safe] : store.options[:safe]

  run_callbacks(:save) do
    run_callbacks(:update) do
      store.client.update(criteria, update, options)
    end
  end
end

#atomic_update_attributes(attrs = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/toy/mongo/querying.rb', line 81

def atomic_update_attributes(attrs={})
  self.attributes = attrs
  if valid?
    run_callbacks(:save) do
      run_callbacks(:update) do
        criteria = {'_id' => id}
        update   = {'$set' => persistable_changes}
        store.client.update(criteria, update, {:safe => store.options[:safe]})
        true
      end
    end
  else
    false
  end
end

#persistable_changesObject

Very basic method for determining what has changed locally so we can just update changes instead of entire document

Does not work with complex objects (array, hash, set, etc.) as it does not attempt to determine what has changed in them, just whether or not they have changed at all.



70
71
72
73
74
75
76
77
78
79
# File 'lib/toy/mongo/querying.rb', line 70

def persistable_changes
  attrs = {}
  pattrs = persisted_attributes
  changed.each do |key|
    attribute = self.class.attributes[key.to_s]
    next if attribute.virtual?
    attrs[attribute.persisted_name] = pattrs[attribute.persisted_name]
  end
  attrs
end