module HasUuid::Mixin::ClassMethods

Public Instance Methods

generate_uuids_if_needed() click to toggle source
# File lib/has_uuid.rb, line 37
def generate_uuids_if_needed
  unless self.uuid
    begin
      self.uuid = UUIDTools::UUID.random_create
    end while !HasUuid.check_uuid(self)
  end
end
has_uuid(options = {}) click to toggle source
# File lib/has_uuid.rb, line 14
def has_uuid(options = {})
  self.class_eval do
    cattr_accessor :has_uuid_options
    self.has_uuid_options = options
    options[:primary_uuid] ||= :uuid

    if options[:primary_uuid] != :uuid
      class_eval do
        include ActiveUUID::UUID

        def uuid
          self.send self.class.primary_uuid
        end

        def uuid=(uuid)
          self.send "#{self.class.primary_uuid}=".to_sym, uuid
        end
      end
    end

    class_eval do
      before_create :generate_uuids_if_needed

      def generate_uuids_if_needed
        unless self.uuid
          begin
            self.uuid = UUIDTools::UUID.random_create
          end while !HasUuid.check_uuid(self)
        end
      end
    end
  end
end
primary_uuid() click to toggle source
# File lib/has_uuid.rb, line 48
def primary_uuid
  self.has_uuid_options[:primary_uuid]
end
uuid() click to toggle source
# File lib/has_uuid.rb, line 24
def uuid
  self.send self.class.primary_uuid
end
uuid=(uuid) click to toggle source
# File lib/has_uuid.rb, line 28
def uuid=(uuid)
  self.send "#{self.class.primary_uuid}=".to_sym, uuid
end