Module: MongoCollection::InstanceMethods

Defined in:
lib/mongo_collection.rb

Instance Method Summary collapse

Instance Method Details

#destroyObject



163
164
165
166
167
168
169
170
171
172
# File 'lib/mongo_collection.rb', line 163

def destroy
    if self.new? 
        return false 
    end
    r = self.class.collection.delete_one(_id: BSON::ObjectId(self._id))
    if !self.class.after_destroy_block.nil?                
        self.class.after_destroy_block.call(self)                
    end
    r
end

#initialize(args = nil) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/mongo_collection.rb', line 112

def initialize(args=nil)       
    self.class.defaults.each do |k, v|                
        self.send(k.to_s+"=", v)                
    end
    if !args.is_a?(Hash)
        return
    end
    args.each do |k,v|                
        if v == nil 
            next
        end
        k_sym = k.to_sym
        k_str = k.to_s 
        new_val = v 

        if self.class.has_one_assosiations.has_key?(k_sym)                
            klass = self.class.has_one_assosiations[k_sym].constantize
            if v.is_a?(Hash)
                new_val = klass.new(v)
            elsif !v.is_a?(klass)
                raise "Value should be a hash or #{klass.to_s}"
            end
        end              
        if self.class.has_many_assosiations.has_key?(k_sym)                    
            klass = self.class.has_many_assosiations[k_sym].constantize
            if !v.is_a?(Array)
                raise "Has many assosiations should be in form of Array"
            end
            
            new_val = v.map do |obj|                    
                new_obj = obj
                if obj.is_a?(Hash)                        
                    new_obj = klass.new(obj)                        
                elsif !obj.is_a?(klass)
                    raise "Value should be a hash or #{klass.to_s}"
                end                          
                new_obj                  
            end

        end
        instance_variable_set "@#{k_str}", new_val if self.class.props.member?(k_sym)
    end
end

#new?Boolean

Returns:

  • (Boolean)


156
157
158
159
160
161
# File 'lib/mongo_collection.rb', line 156

def new?       
    if self._id.blank?
        return true
    end
    !BSON::ObjectId.legal?(self._id)            
end

#saveObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/mongo_collection.rb', line 174

def save
    
    if !self.class.before_save_block.nil?                
        self.class.before_save_block.call(self)                
    end           

    self.class.has_one_assosiations.each do |k,v|
        val = self.send(k)            
        if !val 
            next 
        end
        if val.new? 
            if !val.save 
                raise "Unable to save assosiation #{v}"
            end           
        end
    end
    
    self.class.has_many_assosiations.each do |k,v|
        val = self.send(k)            
        if !val 
            next 
        end                
        val.each do |obj|
            if obj.new? && !obj.save                    
                raise "Unable to save assosiation #{v}"                    
            end    
        end
    end

    if self.new?               
        self._id = nil 
        result = DB[self.class.mongo_collection]
        .insert_one(self.class.to_bson(self))                
        if result.ok?
            self._id = result.inserted_id.to_s
        end
        ok = result.ok?                
    else                 
        ok = DB[self.class.mongo_collection]
        .update_one({_id: BSON::ObjectId(self._id)}, self.class.to_bson(self))
        .ok? 
    end
    if ok && !self.class.after_save_block.nil?                
        self.class.after_save_block.call(self)                                   
    end
    ok
end

#to_hObject



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/mongo_collection.rb', line 223

def to_h
    result = {}        

    self.class.props.each do |prop|            
        value = self.send(prop)
        if value.nil? 
            next
        end      
             
        if self.class.has_one_assosiations.has_key?(prop)
            value = value.to_h            
        end

        if self.class.has_many_assosiations.has_key?(prop)
            value = value.map{|x| x.to_h}
        end
        result[prop] = value
    end
    return result
end