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.18"

Instance Method Summary collapse

Instance Method Details

#find_new_uuid(options = {}) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/mm_uses_uuid.rb', line 116

def find_new_uuid(options = {})
  
  options = {force_safe: false}.merge(options)
    
  if not options[:ensure_unique_in]
    @_id = make_uuid
    #puts "assuming UUID #{@_id} is available"
    return
  else
    find_new_uuid_safely(options[:ensure_unique_in])
  end

end

#find_new_uuid_safely(coll) ⇒ Object



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

def find_new_uuid_safely(coll)

  @_id = nil
  begin
    trial_id = make_uuid
    #puts "CHECKING #{coll} collection for availability of UUID: #{trial_id}"
    if coll.where(:_id => trial_id).fields(:_id).first.nil?
      @_id = trial_id
    end
  end while @_id.nil?

end

#id_to_sObject



152
153
154
155
156
# File 'lib/mm_uses_uuid.rb', line 152

def id_to_s
  copy = self.clone
  copy.instance_variable_set '@_id',  @_id.to_s
  copy
end

#id_to_s!Object



147
148
149
150
# File 'lib/mm_uses_uuid.rb', line 147

def id_to_s!
  @_id = @_id.to_s
  self
end

#make_uuidObject



143
144
145
# File 'lib/mm_uses_uuid.rb', line 143

def make_uuid
  self.class.make_uuid
end