Class: RecordIndex

Inherits:
MongoRecord show all
Extended by:
MongoModel
Defined in:
lib/yodel/models/core/mongo/record_index.rb

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MongoModel

collection, load, scoped

Methods included from AbstractModel

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

Methods inherited from MongoRecord

#collection, #default_values, #fields, #id, #increment!, #inspect_hash, #load_from_mongo, #load_mongo_document, #perform_destroy, #perform_reload, #perform_save, #set_id

Methods inherited from AbstractRecord

#changed!, #changed?, #clear_key, #default_values, #destroy, #destroyed?, #eql?, #errors?, #field, #field?, #field_was, #fields, #from_json, #get, #get_meta, #get_raw, #hash, #id, #increment!, inherited, #initialize, #inspect, #inspect_hash, #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

Class Method Details

.add_index(model_reference, spec) ⇒ Object


Index creation is handled by RecordIndex




15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/yodel/models/core/mongo/record_index.rb', line 15

def self.add_index(model_reference, spec)
  name = index_name(spec)
  index = self.scoped.where(name: name).first
  
  if index.nil?
    index = new
    index.spec = spec
    index.name = name
  end
  
  index.references << model_reference
  index.save
end

.add_index_for_field(model, field) ⇒ Object


Helper methods




45
46
47
48
49
# File 'lib/yodel/models/core/mongo/record_index.rb', line 45

def self.add_index_for_field(model, field)
  name = model_index_name(model, field.name)
  spec = [[field.name, Mongo::ASCENDING]]
  add_index(name, spec)
end

.add_index_for_model(model, name, spec) ⇒ Object



51
52
53
54
# File 'lib/yodel/models/core/mongo/record_index.rb', line 51

def self.add_index_for_model(model, name, spec)
  name = model_index_name(model, name)
  add_index(name, spec)
end

.remove_index(model_reference) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/yodel/models/core/mongo/record_index.rb', line 29

def self.remove_index(model_reference)
  index = self.scoped.where(references: model_reference).first
  return false if index.nil?
  index.references.delete(model_reference)
  
  if index.references.empty?
    index.destroy
  else
    index.save
  end
end

.remove_index_for_field(model, field) ⇒ Object



56
57
58
59
# File 'lib/yodel/models/core/mongo/record_index.rb', line 56

def self.remove_index_for_field(model, field)
  name = model_index_name(model, field.name)
  remove_index(name)
end

.remove_index_for_model(model, name) ⇒ Object



61
62
63
64
# File 'lib/yodel/models/core/mongo/record_index.rb', line 61

def self.remove_index_for_model(model, name)
  name = model_index_name(model, name)
  remove_index(name)
end

Instance Method Details

#create_indexObject



76
77
78
# File 'lib/yodel/models/core/mongo/record_index.rb', line 76

def create_index
  Record.collection.create_index(spec, name: name, background: true)
end

#remove_indexObject



71
72
73
# File 'lib/yodel/models/core/mongo/record_index.rb', line 71

def remove_index
  Record.collection.drop_index(name)
end