Class: PopulateMe::Mongo
Instance Attribute Summary collapse
Attributes inherited from Document
#_is_new, #_old
#_errors
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Document
#==, cast, from_hash, #initialize, #inspect, #nested_docs, #new?, set, #set, #set_defaults, #set_from_hash, #settings, #snapshot, #to_h, #to_s, to_s, to_s_plural, to_s_short, to_s_short_plural
#attachment, #delete, included, #persistent_instance_variables, #save
#error_on, #error_report, #errors, #valid?, #validate
#ensure_delete_attachments, #ensure_delete_related, #ensure_id, #ensure_new, #ensure_not_new, #ensure_position, #exec_callback, included, #recurse_callback
#admin_image_url, included, #to_admin_form, #to_admin_list_item, #to_admin_url
included
#outcast, #outcast_attachment, #outcast_list, #outcast_price, #outcast_select
#typecast, #typecast_attachment, #typecast_date, #typecast_datetime, #typecast_integer, #typecast_price, #typecast_select
Instance Attribute Details
#_id ⇒ Object
Returns the value of attribute _id.
90
91
92
|
# File 'lib/populate_me/mongo.rb', line 90
def _id
@_id
end
|
Class Method Details
.admin_distinct(field, o = {}) ⇒ Object
83
84
85
86
|
# File 'lib/populate_me/mongo.rb', line 83
def admin_distinct field, o={}
query = o.delete(:query) || {}
self.collection.distinct field, query, o
end
|
.admin_find(o = {}) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/populate_me/mongo.rb', line 66
def admin_find o={}
query = o.delete(:query) || {}
o[:sort] ||= @current_sort
if o.key?(:fields)
o[:projection] = o[:fields].inject({}) do |h, f|
h[f.to_sym] = 1
h
end
o.delete(:fields)
end
self.cast{ collection.find(query, o) }
end
|
.admin_find_first(o = {}) ⇒ Object
79
80
81
|
# File 'lib/populate_me/mongo.rb', line 79
def admin_find_first o={}
self.admin_find(o.merge({limit: 1}))[0]
end
|
.admin_get(theid) ⇒ Object
Also known as:
[]
60
61
62
63
|
# File 'lib/populate_me/mongo.rb', line 60
def admin_get theid
theid = BSON::ObjectId.from_string(theid) if BSON::ObjectId.legal?(theid)
self.cast{ collection.find({id_string_key => theid}).first }
end
|
.collection ⇒ Object
17
18
19
20
|
# File 'lib/populate_me/mongo.rb', line 17
def collection
raise MissingMongoDBError, "Document class #{self.name} does not have a Mongo database." if settings.db.nil?
settings.db[settings.collection_name]
end
|
.id_string_key ⇒ Object
44
45
46
|
# File 'lib/populate_me/mongo.rb', line 44
def id_string_key
(self.fields.keys[0]||'_id').to_s
end
|
.inherited(sub) ⇒ Object
12
13
14
15
|
# File 'lib/populate_me/mongo.rb', line 12
def inherited sub
super
sub.set :collection_name, WebUtils.dasherize_class_name(sub.name)
end
|
.set_id_field ⇒ Object
22
23
24
|
# File 'lib/populate_me/mongo.rb', line 22
def set_id_field
field :_id, {type: :id}
end
|
.set_indexes(f, ids = []) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/populate_me/mongo.rb', line 48
def set_indexes f, ids=[]
requests = ids.each_with_index.inject([]) do |list, (id, i)|
list << {update_one:
{
filter: {self.id_string_key=>id},
update: {'$set'=>{f=>i}}
}
}
end
collection.bulk_write requests
end
|
.sort_by(f, direction = 1) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/populate_me/mongo.rb', line 26
def sort_by f, direction=1
direction = 1 if direction==:asc
direction = -1 if direction==:desc
if f.is_a?(Hash)
@current_sort = f
elsif f.is_a?(Array)
@current_sort = f.inject({}) do |h,pair|
h.store(pair[0], pair[1])
h
end
else
raise(ArgumentError) unless [1,-1].include? direction
raise(ArgumentError) unless self.new.respond_to? f
@current_sort = {f => direction}
end
self
end
|
Instance Method Details
#id ⇒ Object
92
|
# File 'lib/populate_me/mongo.rb', line 92
def id; @_id; end
|
#id=(value) ⇒ Object
93
|
# File 'lib/populate_me/mongo.rb', line 93
def id= value; @_id = value; end
|
95
96
97
98
99
100
101
|
# File 'lib/populate_me/mongo.rb', line 95
def perform_create
result = self.class.collection.insert_one(self.to_h)
if result.ok? and self.id.nil?
self.id = result.inserted_id
end
self.id
end
|
108
109
110
|
# File 'lib/populate_me/mongo.rb', line 108
def perform_delete
self.class.collection.delete_one({'_id'=> self.id})
end
|
103
104
105
106
|
# File 'lib/populate_me/mongo.rb', line 103
def perform_update
self.class.collection.update_one({'_id'=> self.id}, self.to_h)
self.id
end
|