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



279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/motion_model/model/model.rb', line 279

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


219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/motion_model/model/model.rb', line 219

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


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

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



294
295
296
# File 'lib/motion_model/model/model.rb', line 294

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

#define_accessor_methods(name) ⇒ Object

nodoc



250
251
252
253
254
255
256
257
258
# File 'lib/motion_model/model/model.rb', line 250

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



260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/motion_model/model/model.rb', line 260

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



273
274
275
276
277
# File 'lib/motion_model/model/model.rb', line 273

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

#has_relation?(col) ⇒ Boolean

nodoc

Returns:

  • (Boolean)


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

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

#issue_notification(object, info) ⇒ Object

nodoc



244
245
246
247
248
# File 'lib/motion_model/model/model.rb', line 244

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

#relation_column?(column) ⇒ Boolean

nodoc

Returns:

  • (Boolean)


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

def relation_column?(column) #nodoc
  [:belongs_to, :belongs_to_id, :has_many].include? column_named(column).type
end

#virtual_relation_column?(column) ⇒ Boolean

nodoc

Returns:

  • (Boolean)


302
303
304
# File 'lib/motion_model/model/model.rb', line 302

def virtual_relation_column?(column) #nodoc
  [:belongs_to, :has_many].include? column_named(column).type
end