module GdatastoreMapper::Associations

Public Class Methods

included(klass) click to toggle source
# File lib/gdatastore_mapper/associations.rb, line 9
def self.included(klass)
  klass.include Associations
end

Public Instance Methods

belongs_to(model) click to toggle source
# File lib/gdatastore_mapper/associations.rb, line 28
def belongs_to model
  add_belongs_to model
  self.class_eval("attr_accessor :#{model.to_s + '_id'}")

  define_method model do
    if self.attributes.include?(id_ model)
      model.to_s.classify.constantize.find self.send(id_ model)
    end
  end
end
belongs_to_models() click to toggle source
# File lib/gdatastore_mapper/associations.rb, line 39
def belongs_to_models
  @belongs_to_models
end
has_many(models) click to toggle source
# File lib/gdatastore_mapper/associations.rb, line 13
def has_many models
  self.class_eval("attr_accessor :#{models.to_s + '_id'}")

  define_method models do
    association = GdatastoreMapper::Associations::HasMany.new(self, models)
    belongings = GdatastoreMapper::Relation.new(self, association)
    if self.attributes.include?(id_ models) && !self.send(id_ models).nil?
      self.send(id_ models).each do |id|
        belongings << (models.to_s.classify.constantize.find id)
      end
    end
    belongings
  end
end

Private Instance Methods

add_belongs_to(model) click to toggle source
# File lib/gdatastore_mapper/associations.rb, line 45
def add_belongs_to(model)
  if @belongs_to_models.nil?
    @belongs_to_models = [model.to_s]
  else
    @belongs_to_models << model.to_s
  end
end