Module: MotionModel::Model::PrivateClassMethods

Defined in:
lib/motion_model/model/model.rb

Instance Method Summary collapse

Instance Method Details

#add_field(name, type, options = {:default => nil}) ⇒ Object

nodoc



331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/motion_model/model/model.rb', line 331

def add_field(name, type, options = {:default => nil}) #nodoc
  col = Column.new(name, type, options)

  @_columns.push col
  @_column_hashes[col.name.to_sym] = col

  case type
    when :has_many then define_has_many_methods(name)
    when :belongs_to then define_belongs_to_methods(name)
    else
      define_accessor_methods(name)
    end
end

#column_from_hash(hash) ⇒ Object

This populates a column from something like:

columns :name => :string, :age => :integer

or

columns :name => {:type => :string, :default => 'Joe Bob'}, :age => :integer


271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/motion_model/model/model.rb', line 271

def column_from_hash(hash) #nodoc
  hash.first.each_pair do |name, options|
    raise ArgumentError.new("you cannot use `description' as a column name because of a conflict with Cocoa.") if name.to_s == 'description'

    case options
    when Symbol, String
      add_field(name, options)
    when Hash
      add_field(name, options[:type], :default => options[:default])
    else
      raise ArgumentError.new("arguments to `columns' must be a symbol, a hash, or a hash of hashes.")
    end
  end
end

#column_from_string_or_sym(string) ⇒ Object

This populates a column from something like:

columns :name, :age, :hobby


290
291
292
293
294
# File 'lib/motion_model/model/model.rb', line 290

def column_from_string_or_sym(string) #nodoc
  string.each do |name|
    add_field(name.to_sym, :string)
  end
end

#column_named(name) ⇒ Object

Returns a column denoted by name



346
347
348
# File 'lib/motion_model/model/model.rb', line 346

def column_named(name) #nodoc
  @_column_hashes[name.to_sym]
end

#define_accessor_methods(name) ⇒ Object

nodoc



302
303
304
305
306
307
308
309
310
# File 'lib/motion_model/model/model.rb', line 302

def define_accessor_methods(name) #nodoc
  define_method(name.to_sym) {
    @data[name]
  }
  define_method("#{name}=".to_sym) { |value|
    @data[name] = cast_to_type(name, value)
    @dirty = true
  }
end

#define_belongs_to_methods(name) ⇒ Object

nodoc



312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/motion_model/model/model.rb', line 312

def define_belongs_to_methods(name) #nodoc
  define_method(name) {
    col = column_named(name)
    parent_id = @data[self.class.generate_belongs_to_id(col.name)]
    col.classify.find(parent_id)
  }
  define_method("#{name}=") { |value|
    col = column_named(name)
    parent_id = self.class.generate_belongs_to_id(col.name)
    @data[parent_id.to_sym] = value.to_i
  }
end

#define_has_many_methods(name) ⇒ Object

nodoc



325
326
327
328
329
# File 'lib/motion_model/model/model.rb', line 325

def define_has_many_methods(name) #nodoc
  define_method(name) {
    relation_for(name)
  }
end

#has_relation?(col) ⇒ Boolean

nodoc

Returns:

  • (Boolean)


365
366
367
368
369
370
371
372
373
374
375
# File 'lib/motion_model/model/model.rb', line 365

def has_relation?(col) #nodoc
  return false if col.nil?

  col = case col
  when MotionModel::Model::Column
    column_named(col.name)
  else
    column_named(col)
  end
  col.type == :has_many || col.type == :belongs_to
end

#increment_idObject

Increments next available id



361
362
363
# File 'lib/motion_model/model/model.rb', line 361

def increment_id #nodoc
  @_next_id += 1
end

#issue_notification(object, info) ⇒ Object

nodoc



296
297
298
299
300
# File 'lib/motion_model/model/model.rb', line 296

def issue_notification(object, info) #nodoc
  if @_issue_notifications == true && !object.nil?
    NSNotificationCenter.defaultCenter.postNotificationName('MotionModelDataDidChangeNotification', object: object, userInfo: info)
  end
end

#next_idObject

Returns next available id



351
352
353
# File 'lib/motion_model/model/model.rb', line 351

def next_id #nodoc
  @_next_id
end

#next_id=(value) ⇒ Object

Sets next available id



356
357
358
# File 'lib/motion_model/model/model.rb', line 356

def next_id=(value) #nodoc
  @_next_id = value
end