Class: MongoRecord

Inherits:
AbstractRecord show all
Extended by:
MongoModel
Defined in:
lib/yodel/models/core/record/mongo_record.rb

Direct Known Subclasses

RecordIndex, Remote, Site, SiteRecord

Constant Summary

Constants inherited from AbstractRecord

AbstractRecord::CALLBACKS, AbstractRecord::FIELD_CALLBACKS, AbstractRecord::ORDERS

Instance Attribute Summary

Attributes inherited from AbstractRecord

#changed, #errors, #stash, #typecast, #values

Instance Method Summary collapse

Methods included from MongoModel

load, scoped

Methods included from AbstractModel

#embed_many, #embed_one, #field, #many, #modify_field, #one, #remove_field

Methods inherited from AbstractRecord

#changed!, #changed?, #clear_key, #destroy, #destroyed?, #eql?, #errors?, #field, #field?, #field_was, #from_json, #get, #get_meta, #get_raw, #hash, inherited, #initialize, #inspect, #inspect_value, #method_missing, #new?, #prepare_reload_params, #present?, #reload, #save, #save_without_validation, #search_terms, #set, #set_meta, #set_raw, #to_json, #to_str, #trigger_field_callback, #update, #valid?

Constructor Details

This class inherits a constructor from AbstractRecord

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class AbstractRecord

Instance Method Details

#collectionObject



11
12
13
# File 'lib/yodel/models/core/record/mongo_record.rb', line 11

def collection
  self.class.collection
end

#default_valuesObject



23
24
25
# File 'lib/yodel/models/core/record/mongo_record.rb', line 23

def default_values
  super.merge({'_id' => PrimaryKeyFactory.pk})
end

#fieldsObject



7
8
9
# File 'lib/yodel/models/core/record/mongo_record.rb', line 7

def fields
  self.class.fields
end

#idObject



15
16
17
# File 'lib/yodel/models/core/record/mongo_record.rb', line 15

def id
  @values['_id']
end

#increment!(name, value = 1, conditions = {}) ⇒ Object

Raises:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/yodel/models/core/record/mongo_record.rb', line 57

def increment!(name, value=1, conditions={})
  name = name.to_s
  
  # preconditions
  raise DestroyedRecord if destroyed?
  raise UnknownField, "Unknown field <#{name}>" unless field?(name)
  return false if new?
  
  increment_field = field(name)
  raise InvalidField, "Field #{name} is not numeric" unless increment_field.numeric?
  
  # atomic increment (amount can be negative)
  conditions = {_id: id}.merge(Plucky::CriteriaHash.new(conditions).to_hash)
  result = collection.update(conditions, {'$inc' => {name => value}}, safe: true)
  succeeded = successful_result?(result)
  
  # update the object cache, and indicate if the update was successful
  new_value = (get(name) || 0) + value
  @values[name] = @typecast[name] = new_value if succeeded
  succeeded
end

#inspect_hashObject



27
28
29
# File 'lib/yodel/models/core/record/mongo_record.rb', line 27

def inspect_hash
  {id: id}.merge(super)
end

#load_from_mongo(scope) ⇒ Object



53
54
55
# File 'lib/yodel/models/core/record/mongo_record.rb', line 53

def load_from_mongo(scope)
  @values = load_mongo_document(scope)
end

#load_mongo_document(scope) ⇒ Object



49
50
51
# File 'lib/yodel/models/core/record/mongo_record.rb', line 49

def load_mongo_document(scope)
  collection.find_one(scope)
end

#perform_destroyObject



38
39
40
41
42
# File 'lib/yodel/models/core/record/mongo_record.rb', line 38

def perform_destroy
  result = collection.remove(_id: @values['_id'])
rescue
  false
end

#perform_reload(params) ⇒ Object



44
45
46
47
# File 'lib/yodel/models/core/record/mongo_record.rb', line 44

def perform_reload(params)
  document = load_mongo_document(_id: params[:id])
  initialize(document)
end

#perform_saveObject



31
32
33
34
35
36
# File 'lib/yodel/models/core/record/mongo_record.rb', line 31

def perform_save
  id = collection.save(@values, safe: true)
rescue
  # TODO: write Yodel.db.get_last_error to the log or as a warning to the site
  false
end

#set_id(new_id) ⇒ Object



19
20
21
# File 'lib/yodel/models/core/record/mongo_record.rb', line 19

def set_id(new_id)
  @values['_id'] = new_id
end