Class: Minidoc

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion, ActiveModel::Validations, Connection, Finders, Validations
Defined in:
lib/minidoc.rb,
lib/minidoc/counters.rb,
lib/minidoc/test_helpers.rb

Defined Under Namespace

Modules: Associations, Connection, Counters, Finders, Indexes, TestHelpers, Timestamps, Validations Classes: DuplicateKey, Grid, ReadOnly, RecordInvalid, Value

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Minidoc

Returns a new instance of Minidoc.



125
126
127
128
129
130
131
132
133
134
# File 'lib/minidoc.rb', line 125

def initialize(attrs = {})
  if attrs["_id"].nil? && attrs[:_id].nil?
    attrs[:_id] = BSON::ObjectId.new
  end

  @new_record = true
  @destroyed = false

  super(attrs)
end

Class Method Details

.atomic_set(query, attributes) ⇒ Object



69
70
71
72
# File 'lib/minidoc.rb', line 69

def self.atomic_set(query, attributes)
  result = collection.update(query, "$set" => attributes)
  result["ok"] == 1 && result["n"] == 1
end

.create(attrs = {}) ⇒ Object



37
38
39
# File 'lib/minidoc.rb', line 37

def self.create(attrs = {})
  new(attrs).tap(&:save)
end

.create!(*args) ⇒ Object



41
42
43
# File 'lib/minidoc.rb', line 41

def self.create!(*args)
  new(*args).tap(&:save!)
end

.delete(id) ⇒ Object



45
46
47
# File 'lib/minidoc.rb', line 45

def self.delete(id)
  collection.remove(_id: BSON::ObjectId(id.to_s))
end

.delete_allObject



33
34
35
# File 'lib/minidoc.rb', line 33

def self.delete_all
  collection.remove({})
end

.rescue_duplicate_key_errorsObject

Rescue a duplicate key exception in the given block. Returns the result of the block, or false if the exception was raised.



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/minidoc.rb', line 108

def self.rescue_duplicate_key_errors
  yield
rescue Minidoc::DuplicateKey
  false
rescue Mongo::OperationFailure => ex
  if Minidoc::DuplicateKey.duplicate_key_exception(ex)
    false
  else
    raise
  end
end

.set(id, attributes) ⇒ Object



49
50
51
52
# File 'lib/minidoc.rb', line 49

def self.set(id, attributes)
  id = BSON::ObjectId(id.to_s)
  update_one(id, "$set" => attributes)
end

.tokumx?Boolean

Returns:

  • (Boolean)


120
121
122
123
# File 'lib/minidoc.rb', line 120

def self.tokumx?
  @server_info ||= connection.server_info
  @server_info.key?("tokumxVersion")
end

.transaction(isolation = "mvcc") ⇒ Object

For databases that support it (e.g. TokuMX), perform the block within a transaction. For information on the isolation argument, see www.percona.com/doc/percona-tokumx/commands.html#beginTransaction



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/minidoc.rb', line 87

def self.transaction(isolation = "mvcc")
  return yield unless tokumx?

  begin
    database.command(beginTransaction: 1, isolation: isolation)
    yield
  rescue Exception => error
    database.command(rollbackTransaction: 1) rescue nil
    raise
  ensure
    begin
      database.command(commitTransaction: 1) unless error
    rescue Exception
      database.command(rollbackTransaction: 1)
      raise
    end
  end
end

.unset(id, *keys) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/minidoc.rb', line 54

def self.unset(id, *keys)
  id = BSON::ObjectId(id.to_s)

  unsets = {}
  keys.each do |key|
    unsets[key] = 1
  end

  update_one(id, "$unset" => unsets)
end

.update_one(id, updates) ⇒ Object



65
66
67
# File 'lib/minidoc.rb', line 65

def self.update_one(id, updates)
  collection.update({ "_id" => id }, updates)
end

.value_classObject



74
75
76
77
78
79
80
81
82
# File 'lib/minidoc.rb', line 74

def self.value_class
  @value_class ||= Class.new(self) do
    attribute_set.each do |attr|
      private "#{attr.name}="
    end

    private :attributes=
  end
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  other.is_a?(self.class) && self.id && self.id == other.id
end

#as_valueObject



207
208
209
# File 'lib/minidoc.rb', line 207

def as_value
  self.class.value_class.new(attributes)
end

#atomic_set(query, attributes) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
# File 'lib/minidoc.rb', line 195

def atomic_set(query, attributes)
  query[:_id] = id

  if self.class.atomic_set(query, attributes)
    attributes.each do |name, value|
      self[name] = value
    end

    true
  end
end

#deleteObject



152
153
154
# File 'lib/minidoc.rb', line 152

def delete
  self.class.delete(id)
end

#destroyObject



156
157
158
159
# File 'lib/minidoc.rb', line 156

def destroy
  delete
  @destroyed = true
end

#destroyed?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/minidoc.rb', line 144

def destroyed?
  @destroyed
end

#new_record?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/minidoc.rb', line 140

def new_record?
  @new_record
end

#persisted?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/minidoc.rb', line 148

def persisted?
  !(new_record? || destroyed?)
end

#reloadObject



161
162
163
164
165
166
167
168
169
# File 'lib/minidoc.rb', line 161

def reload
  new_object = self.class.find(self.id)

  self.class.attribute_set.each do |attr|
    self[attr.name] = new_object[attr.name]
  end

  self
end

#saveObject



171
172
173
# File 'lib/minidoc.rb', line 171

def save
  valid? ? create_or_update : false
end

#save!Object



175
176
177
# File 'lib/minidoc.rb', line 175

def save!
  valid? ? create_or_update : raise(RecordInvalid.new(self))
end

#set(attributes) ⇒ Object



179
180
181
182
183
184
185
# File 'lib/minidoc.rb', line 179

def set(attributes)
  self.class.set(id, attributes)

  attributes.each do |name, value|
    self[name] = value
  end
end

#to_keyObject



211
212
213
# File 'lib/minidoc.rb', line 211

def to_key
  [id.to_s]
end

#to_paramObject



215
216
217
# File 'lib/minidoc.rb', line 215

def to_param
  id.to_s
end

#unset(*keys) ⇒ Object



187
188
189
190
191
192
193
# File 'lib/minidoc.rb', line 187

def unset(*keys)
  self.class.unset(id, *keys)

  keys.each do |key|
    self[key] = nil
  end
end