Sha256: 4a9d091f5cab835757ecd0ad677c54bce1377fe07c27c567a434a073857d1aae

Contents?: true

Size: 1.06 KB

Versions: 2

Compression:

Stored size: 1.06 KB

Contents

require 'jinx/resource/uniquifier_cache'

module Jinx
  # The Unique mix-in makes values unique within the scope of a Resource class.
  module Unique
    # Replaces each String secondary and alternate key property with a unique
    # value. Successive calls to this method for domain objects of the same class
    # replace the same String key property values with the same unique value. 
    #
    # @return [Resource] self
    def uniquify
      uniquify_attributes(self.class.secondary_key_attributes)
      uniquify_attributes(self.class.alternate_key_attributes)
      self
    end

    private

    # Makes this domain object's String values for the given attributes unique.
    #
    # @param [<Symbol>] the key attributes to uniquify
    def uniquify_attributes(attributes)
      attributes.each do |ka|
        oldval = send(ka)
        next unless String === oldval
        newval = UniquifierCache.instance.get(self, oldval)
        set_property_value(ka, newval)
        logger.debug { "Reset #{qp} #{ka} from #{oldval} to unique value #{newval}." }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jinx-2.1.4 lib/jinx/resource/unique.rb
jinx-2.1.3 lib/jinx/resource/unique.rb