Module: ActiveUUID::UUID

Extended by:
ActiveSupport::Concern
Defined in:
lib/activeuuid/uuid.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#create_uuidObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/activeuuid/uuid.rb', line 129

def create_uuid
  if _natural_key
    # TODO if all the attributes return nil you might want to warn about this
    chained = _natural_key.map { |attribute| self.send(attribute) }.join('-')
    UUIDTools::UUID.sha1_create(_uuid_namespace || UUIDTools::UUID_OID_NAMESPACE, chained)
  else
    case _uuid_generator
    when :random
      UUIDTools::UUID.random_create
    when :time
      UUIDTools::UUID.timestamp_create
    end
  end
end

#generate_uuids_if_neededObject



144
145
146
147
148
149
# File 'lib/activeuuid/uuid.rb', line 144

def generate_uuids_if_needed
  primary_key = self.class.primary_key
  if self.class.columns_hash[primary_key].type == :uuid
    send("#{primary_key}=", create_uuid) unless send("#{primary_key}?")
  end
end