Module: LeanMotion::Model::ClassMethods

Defined in:
lib/lean_motion/model.rb

Instance Method Summary collapse

Instance Method Details

#all(&block) ⇒ Object



230
231
232
233
234
235
236
237
# File 'lib/lean_motion/model.rb', line 230

def all(&block)
  cloud_query = query
  return cloud_query.find unless block_given?
  
  cloud_query.find do |objects, error|
    block.call(objects, error)
  end
end

#count(&block) ⇒ Object



221
222
223
224
225
226
227
228
# File 'lib/lean_motion/model.rb', line 221

def count(&block)
  cloud_query = query
  return cloud_query.countObjects unless block_given?

  cloud_query.count do |count, error|
    block.call(count, error)
  end
end

#destroy(&block) ⇒ Object



253
254
255
256
257
258
259
260
# File 'lib/lean_motion/model.rb', line 253

def destroy(&block)
  cloud_query = query
  return cloud_query.delete unless block_given?

  cloud_query.destroy do |object, error|
    block.call(object, error)
  end
end

#field(name) ⇒ Object

set a field for the current Model

(see #fields)

Parameters:

  • name (Symbol)

    of field



284
285
286
287
288
# File 'lib/lean_motion/model.rb', line 284

def field(name)
  @fields ||= [:objectId]
  @fields << name.to_sym
  @fields.uniq!
end

#fields(*args) ⇒ Object

set the fields for the current Model

used in method_missing

Parameters:

  • args (Symbol)

    one or more fields



276
277
278
# File 'lib/lean_motion/model.rb', line 276

def fields(*args)
  args.each {|arg| field(arg)}
end

#first(&block) ⇒ Object



244
245
246
247
248
249
250
251
# File 'lib/lean_motion/model.rb', line 244

def first(&block)
  cloud_query = query
  return cloud_query.first unless block_given?

  cloud_query.first do |object, error|
    block.call(object, error)
  end
end

#get_fieldsObject



290
291
292
# File 'lib/lean_motion/model.rb', line 290

def get_fields
  @fields ||= []
end

#get_presence_validation_messagesObject



307
308
309
# File 'lib/lean_motion/model.rb', line 307

def get_presence_validation_messages
  @presenceValidationMessages ||= {}
end

#get_presence_validationsObject



303
304
305
# File 'lib/lean_motion/model.rb', line 303

def get_presence_validations
  @presenceValidations ||= {}
end

#page(number, pagesize = 20) ⇒ Object



267
268
269
270
# File 'lib/lean_motion/model.rb', line 267

def page(number, pagesize=20)
  cloud_query = query
  cloud_query.page(number, pagesize)
end

#queryObject



217
218
219
# File 'lib/lean_motion/model.rb', line 217

def query
    LeanMotion::CloudQuery.alloc.initWithClassNameAndClassObject(self.name, classObject:self)
end

#sort(hash) ⇒ Object



262
263
264
265
# File 'lib/lean_motion/model.rb', line 262

def sort(hash)
  cloud_query = query
  cloud_query.sort(hash)
end

#validate_presence(field) ⇒ Object



311
312
313
314
# File 'lib/lean_motion/model.rb', line 311

def validate_presence(field)
  @presenceValidations ||= []
  @presenceValidations << field
end

#validates_presence_of(field, opts = {}) ⇒ Object

require a certain field to be present (not nil And not an empty String)

Parameters:

  • field (Symbol, Hash)

    and options (now only has message)



297
298
299
300
301
# File 'lib/lean_motion/model.rb', line 297

def validates_presence_of(field, opts={})
  @presenceValidationMessages ||= {}
  @presenceValidationMessages[field] = opts[:message] if opts[:message]
  validate_presence(field)
end

#where(hash) ⇒ Object



239
240
241
242
# File 'lib/lean_motion/model.rb', line 239

def where(hash)
  cloud_query = query
  cloud_query.findByHash(hash)
end