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/version.rb,
lib/minidoc/counters.rb,
lib/minidoc/test_helpers.rb

Defined Under Namespace

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

Constant Summary collapse

VERSION =
"1.0.1".freeze

Constants included from Finders

Finders::DocumentNotFoundError

Constants included from Connection

Connection::MissingConfiguration

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Minidoc

Returns a new instance of Minidoc.



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

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



67
68
69
70
# File 'lib/minidoc.rb', line 67

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

.create(attrs = {}) ⇒ Object



35
36
37
# File 'lib/minidoc.rb', line 35

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

.create!(*args) ⇒ Object



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

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

.delete(id) ⇒ Object



43
44
45
# File 'lib/minidoc.rb', line 43

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

.delete_allObject



31
32
33
# File 'lib/minidoc.rb', line 31

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.



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

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



47
48
49
50
# File 'lib/minidoc.rb', line 47

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

.tokumx?Boolean

Returns:

  • (Boolean)


118
119
120
121
# File 'lib/minidoc.rb', line 118

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



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

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



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

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



63
64
65
# File 'lib/minidoc.rb', line 63

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

.value_classObject



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

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



134
135
136
# File 'lib/minidoc.rb', line 134

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

#as_valueObject



205
206
207
# File 'lib/minidoc.rb', line 205

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

#atomic_set(query, attributes) ⇒ Object



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

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



150
151
152
# File 'lib/minidoc.rb', line 150

def delete
  self.class.delete(id)
end

#destroyObject



154
155
156
157
# File 'lib/minidoc.rb', line 154

def destroy
  delete
  @destroyed = true
end

#destroyed?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/minidoc.rb', line 142

def destroyed?
  @destroyed
end

#new_record?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/minidoc.rb', line 138

def new_record?
  @new_record
end

#persisted?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/minidoc.rb', line 146

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

#reloadObject



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

def reload
  new_object = self.class.find(self.id) or raise DocumentNotFoundError

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

  self
end

#saveObject



169
170
171
# File 'lib/minidoc.rb', line 169

def save
  valid? ? create_or_update : false
end

#save!Object



173
174
175
# File 'lib/minidoc.rb', line 173

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

#set(attributes) ⇒ Object



177
178
179
180
181
182
183
# File 'lib/minidoc.rb', line 177

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

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

#to_keyObject



209
210
211
# File 'lib/minidoc.rb', line 209

def to_key
  [id.to_s]
end

#to_paramObject



213
214
215
# File 'lib/minidoc.rb', line 213

def to_param
  id.to_s
end

#unset(*keys) ⇒ Object



185
186
187
188
189
190
191
# File 'lib/minidoc.rb', line 185

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

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