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 =
"2.1.0".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.



96
97
98
99
100
101
102
103
104
105
# File 'lib/minidoc.rb', line 96

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.



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/minidoc.rb', line 84

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

.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



107
108
109
# File 'lib/minidoc.rb', line 107

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

#as_valueObject



178
179
180
# File 'lib/minidoc.rb', line 178

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

#atomic_set(query, attributes) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/minidoc.rb', line 166

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



123
124
125
# File 'lib/minidoc.rb', line 123

def delete
  self.class.delete(id)
end

#destroyObject



127
128
129
130
# File 'lib/minidoc.rb', line 127

def destroy
  delete
  @destroyed = true
end

#destroyed?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/minidoc.rb', line 115

def destroyed?
  @destroyed
end

#new_record?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/minidoc.rb', line 111

def new_record?
  @new_record
end

#persisted?Boolean

Returns:

  • (Boolean)


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

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

#reloadObject



132
133
134
135
136
137
138
139
140
# File 'lib/minidoc.rb', line 132

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



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

def save
  valid? ? create_or_update : false
end

#save!Object



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

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

#set(attributes) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/minidoc.rb', line 150

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

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

#to_keyObject



182
183
184
# File 'lib/minidoc.rb', line 182

def to_key
  [id.to_s]
end

#to_paramObject



186
187
188
# File 'lib/minidoc.rb', line 186

def to_param
  id.to_s
end

#unset(*keys) ⇒ Object



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

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

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