Module: MmUsesUuid
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/mm_uses_uuid/version.rb,
lib/mm_uses_uuid.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- VERSION =
"0.0.15"
Instance Method Summary collapse
- #find_new_uuid(options = {}) ⇒ Object
- #find_new_uuid_safely(coll) ⇒ Object
- #id_to_s ⇒ Object
- #id_to_s! ⇒ Object
- #make_uuid ⇒ Object
Instance Method Details
#find_new_uuid(options = {}) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/mm_uses_uuid.rb', line 127 def find_new_uuid( = {}) = {force_safe: false}.merge() if not [:ensure_unique_in] @_id, variant = make_uuid #puts "assuming #{variant} UUID #{@_id} is available" return else find_new_uuid_safely([:ensure_unique_in]) end end |
#find_new_uuid_safely(coll) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/mm_uses_uuid.rb', line 141 def find_new_uuid_safely(coll) @_id = nil begin trial_id, variant = make_uuid #puts "CHECKING #{coll} collection for availability of #{variant} UUID: #{trial_id}" if coll.where(:_id => trial_id).fields(:_id).first.nil? @_id = trial_id end end while @_id.nil? end |
#id_to_s ⇒ Object
173 174 175 176 177 |
# File 'lib/mm_uses_uuid.rb', line 173 def id_to_s copy = self.clone copy.instance_variable_set '@_id', @_id.to_s copy end |
#id_to_s! ⇒ Object
168 169 170 171 |
# File 'lib/mm_uses_uuid.rb', line 168 def id_to_s! @_id = @_id.to_s self end |
#make_uuid ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/mm_uses_uuid.rb', line 154 def make_uuid uuid = SecureRandom.uuid.gsub!('-', '') if self.class.single_collection_inherited? lookup_class = self.class.collection_name.singularize.camelize.constantize else lookup_class = self.class end if replacement_lsn = UuidModel.class_lsn_lookup[lookup_class] uuid[-1] = replacement_lsn.to_s(16) end bson_encoded_uuid = BSON::Binary.new(uuid, BSON::Binary::SUBTYPE_UUID) return bson_encoded_uuid, 'random' end |