Module: Jinx::Unique

Defined in:
lib/jinx/resource/unique.rb

Overview

The Unique mix-in makes values unique within the scope of a Resource class.

Instance Method Summary collapse

Instance Method Details

#uniquifyObject

Makes the secondary key unique by replacing each String key attribute value with a unique value.



18
19
20
21
# File 'lib/jinx/resource/unique.rb', line 18

def uniquify
  uniquify_attributes(self.class.secondary_key_attributes)
  uniquify_attributes(self.class.alternate_key_attributes)
end

#uniquify_attributes(attributes) ⇒ Object

Makes the given attribute values unique by replacing each String value with a unique value.



25
26
27
28
29
30
31
32
33
# File 'lib/jinx/resource/unique.rb', line 25

def uniquify_attributes(attributes)
  attributes.each do |ka|
    oldval = send(ka)
    next unless String === oldval
    newval = uniquify_value(oldval)
    set_property_value(ka, newval)
    logger.debug { "Reset #{qp} #{ka} from #{oldval} to unique value #{newval}." }
  end
end

#uniquify_value(value) ⇒ Object

Makes the given String value unique in the context of this object’s class. Raises TypeError if value is neither nil nor a String.

Returns:

  • nil if value is nil



9
10
11
12
13
14
# File 'lib/jinx/resource/unique.rb', line 9

def uniquify_value(value)
  unless String === value or value.nil? then
    raise TypeError.new("Cannot uniquify #{qp} non-String value #{value}")
  end
  Uniquifier.instance.uniquify(self, value)
end