Class: MongoRecord
Constant Summary
AbstractRecord::CALLBACKS, AbstractRecord::FIELD_CALLBACKS, AbstractRecord::ORDERS
Instance Attribute Summary
#changed, #errors, #stash, #typecast, #values
Instance Method Summary
collapse
Methods included from MongoModel
load, scoped
#embed_many, #embed_one, #field, #many, #modify_field, #one, #remove_field
#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
#collection ⇒ Object
11
12
13
|
# File 'lib/yodel/models/core/record/mongo_record.rb', line 11
def collection
self.class.collection
end
|
#default_values ⇒ Object
23
24
25
|
# File 'lib/yodel/models/core/record/mongo_record.rb', line 23
def default_values
super.merge({'_id' => PrimaryKeyFactory.pk})
end
|
#fields ⇒ Object
7
8
9
|
# File 'lib/yodel/models/core/record/mongo_record.rb', line 7
def fields
self.class.fields
end
|
#id ⇒ Object
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
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
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?
conditions = {_id: id}.merge(Plucky::CriteriaHash.new(conditions).to_hash)
result = collection.update(conditions, {'$inc' => {name => value}}, safe: true)
succeeded = successful_result?(result)
new_value = (get(name) || 0) + value
@values[name] = @typecast[name] = new_value if succeeded
succeeded
end
|
#inspect_hash ⇒ Object
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
|
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
|
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
|
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
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
|