module ActiveMongoid::BsonId::ClassMethods

Public Instance Methods

bsonify_attr(name, options = {}) click to toggle source
# File lib/active_mongoid/bson_id.rb, line 13
def bsonify_attr(name, options = {})
  bson_attr_setter(name)
  bson_attr_getter(name) unless options[:primary]
  bson_attrs << name
  bson_attr_init(name) if options[:initialize]
  set_ar_primary_bson_id(name) if options[:primary]
end
set_ar_primary_bson_id(name) click to toggle source
# File lib/active_mongoid/bson_id.rb, line 21
def set_ar_primary_bson_id(name)
  self.primary_key = name
  self.primary_bson_key = name
  alias_attribute "_#{name}".to_sym, name
end

Private Instance Methods

bson_attr_getter(name) click to toggle source
# File lib/active_mongoid/bson_id.rb, line 38
def bson_attr_getter(name)
  self.instance_eval do
    define_method(name) do
      attribute = read_attribute(name)
      attribute.nil? ? nil : ::ActiveMongoid::BSON::ObjectId.from_string(attribute.to_s)
    end
  end
end
bson_attr_init(name) click to toggle source
# File lib/active_mongoid/bson_id.rb, line 47
def bson_attr_init(name)
  init_method = :"init_attr_for_#{name}"
  define_method(init_method) do
    self.send("#{name}=", ::ActiveMongoid::BSON::ObjectId.new) unless read_attribute(name)
  end
  after_initialize init_method
end
bson_attr_setter(name) click to toggle source
# File lib/active_mongoid/bson_id.rb, line 29
def bson_attr_setter(name)
  self.instance_eval do
    define_method("#{name}=") do |object|
      attribute = object.nil? ? nil : object.to_s
      write_attribute(name, attribute)
    end
  end
end