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



323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/motion_model/model/model.rb', line 323

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


263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/motion_model/model/model.rb', line 263

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.delete(:type), options)
    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


282
283
284
285
286
# File 'lib/motion_model/model/model.rb', line 282

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



338
339
340
# File 'lib/motion_model/model/model.rb', line 338

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

#define_accessor_methods(name) ⇒ Object

nodoc



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

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



304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/motion_model/model/model.rb', line 304

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



317
318
319
320
321
# File 'lib/motion_model/model/model.rb', line 317

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

#has_relation?(col) ⇒ Boolean

nodoc

Returns:

  • (Boolean)


357
358
359
360
361
362
363
364
365
366
367
# File 'lib/motion_model/model/model.rb', line 357

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



353
354
355
# File 'lib/motion_model/model/model.rb', line 353

def increment_id #nodoc
  @_next_id += 1
end

#issue_notification(object, info) ⇒ Object

nodoc



288
289
290
291
292
# File 'lib/motion_model/model/model.rb', line 288

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



343
344
345
# File 'lib/motion_model/model/model.rb', line 343

def next_id #nodoc
  @_next_id
end

#next_id=(value) ⇒ Object

Sets next available id



348
349
350
# File 'lib/motion_model/model/model.rb', line 348

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