Class: PopulateMe::Mongo

Inherits:
Document show all
Defined in:
lib/populate_me/mongo.rb

Instance Attribute Summary collapse

Attributes inherited from Document

#_is_new, #_old

Attributes included from DocumentMixins::Validation

#_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

Methods included from DocumentMixins::Persistence

#attachment, #delete, included, #persistent_instance_variables, #save

Methods included from DocumentMixins::Validation

#error_on, #error_report, #errors, #valid?, #validate

Methods included from DocumentMixins::Callbacks

#ensure_delete_attachments, #ensure_delete_related, #ensure_id, #ensure_new, #ensure_not_new, #ensure_position, #exec_callback, included, #recurse_callback

Methods included from DocumentMixins::AdminAdapter

#admin_image_url, included, #to_admin_form, #to_admin_list_item, #to_admin_url

Methods included from DocumentMixins::Schema

#field_applicable?, included, #relationship_applicable?

Methods included from DocumentMixins::Outcasting

#outcast, #outcast_attachment, #outcast_list, #outcast_price, #outcast_select, #outcast_string

Methods included from DocumentMixins::Typecasting

#typecast, #typecast_attachment, #typecast_date, #typecast_datetime, #typecast_integer, #typecast_price, #typecast_select

Constructor Details

This class inherits a constructor from PopulateMe::Document

Instance Attribute Details

#_idObject

Returns the value of attribute _id.



136
137
138
# File 'lib/populate_me/mongo.rb', line 136

def _id
  @_id
end

Class Method Details

.admin_distinct(field, o = {}) ⇒ Object



121
122
123
124
# File 'lib/populate_me/mongo.rb', line 121

def admin_distinct field, o={}
  query = o.delete(:query) || {}
  self.collection.distinct field, query, o
end

.admin_find(o = {}) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/populate_me/mongo.rb', line 104

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



117
118
119
# File 'lib/populate_me/mongo.rb', line 117

def admin_find_first o={}
  self.admin_find(o.merge({limit: 1}))[0]
end

.admin_get(theid) ⇒ Object Also known as: []



90
91
92
93
94
# File 'lib/populate_me/mongo.rb', line 90

def admin_get theid
  return self.admin_get_multiple(theid) if theid.is_a?(Array)
  theid = string_or_object_id theid
  self.cast{ collection.find({id_string_key => theid}).first }
end

.admin_get_multiple(theids, o = {sort: nil}) ⇒ Object



97
98
99
100
101
102
# File 'lib/populate_me/mongo.rb', line 97

def admin_get_multiple theids, o={sort: nil}
  theids = theids.uniq.compact.map{|theid| string_or_object_id(theid) }
  self.admin_find(o.merge({
    query: {id_string_key => {'$in' => theids} }
  }))
end

.collectionObject



43
44
45
46
# File 'lib/populate_me/mongo.rb', line 43

def collection
  raise MissingMongoDBError, "Document class #{self.name} does not have a Mongo database." if settings.db.nil?
  settings.db[self.collection_name]
end

.collection_nameObject



39
40
41
# File 'lib/populate_me/mongo.rb', line 39

def collection_name
  self.settings.collection_name || WebUtils.dasherize_class_name(self.name) 
end

.id_string_keyObject



70
71
72
# File 'lib/populate_me/mongo.rb', line 70

def id_string_key
  (self.fields.keys[0]||'_id').to_s
end

.inherited(sub) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/populate_me/mongo.rb', line 23

def inherited sub 
  super
  # self.inherited is not useful anymore because we use ::collection_name 
  # so that the class name exist when this is run.
  # Which is not the case with dynamically created classes.
  #
  # But we'll keep it for legacy code in the meantime.
  # Decide if we want to keep it.
  #
  # If statment is here for dynamically created classes,
  # because Class.new.name==nil and then it breaks (see tests).
  unless sub.name.nil?
    sub.set :collection_name, WebUtils.dasherize_class_name(sub.name)
  end
end

.set_id_fieldObject



48
49
50
# File 'lib/populate_me/mongo.rb', line 48

def set_id_field
  field :_id, {type: :id}
end

.set_indexes(f, ids = []) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/populate_me/mongo.rb', line 74

def set_indexes f, ids=[]
  if self.fields[f.to_sym][:direction]==:desc
    ids = ids.dup.reverse
  end
  requests = ids.each_with_index.inject([]) do |list, (theid, i)|
    theid = string_or_object_id theid
    list << {update_one: 
      {
        filter: {self.id_string_key=>theid}, 
        update: {'$set'=>{f=>i}}
      }
    }
  end
  collection.bulk_write requests
end

.sort_by(f, direction = 1) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/populate_me/mongo.rb', line 52

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

.string_or_object_id(theid) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/populate_me/mongo.rb', line 126

def string_or_object_id theid
  if BSON::ObjectId.legal?(theid)
    BSON::ObjectId.from_string(theid)
  else
    theid
  end
end

Instance Method Details

#idObject



138
# File 'lib/populate_me/mongo.rb', line 138

def id; @_id; end

#id=(value) ⇒ Object



139
# File 'lib/populate_me/mongo.rb', line 139

def id= value; @_id = value; end

#perform_createObject



141
142
143
144
145
146
147
# File 'lib/populate_me/mongo.rb', line 141

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

#perform_deleteObject



154
155
156
# File 'lib/populate_me/mongo.rb', line 154

def perform_delete
  self.class.collection.delete_one({'_id'=> self.id})
end

#perform_updateObject



149
150
151
152
# File 'lib/populate_me/mongo.rb', line 149

def perform_update
  self.class.collection.update_one({'_id'=> self.id}, self.to_h)
  self.id
end