Sha256: c35b1b71edcb22055683a750ed240b5cfee0a88c5e1775dd75e728bccfa48dee

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

require 'singleton'
require 'caruby/util/uniquifier'
require 'caruby/util/collection'

module CaRuby
  module Resource
    # The Unique mix-in makes values unique within the scope of a Resource class.
    module Unique
      # Makes the given String value unique in the context of this object's class.
      # @return nil if value is nil
      # Raises TypeError if value is neither nil nor a String.
      def uniquify_value(value)
        unless String === value or value.nil? then
          raise TypeError.new("Cannot uniquify #{qp} non-String value #{value}")
        end
        ResourceUniquifier.instance.uniquify(self, value)
      end
    end
  end
  
  # The ResourceUniquifier singleton makes Resource instance attribute values unique.
  class ResourceUniquifier
    include Singleton

    def initialize
      @cache = LazyHash.new { Hash.new }
    end

    # Makes the obj attribute value unique, or returns nil if value is nil.
    def uniquify(obj, value)
      @cache[obj.class][value] ||= value.uniquify if value
    end

    def clear
      @cache.clear
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
caruby-core-1.4.3 lib/caruby/domain/uniquify.rb
caruby-core-1.4.2 lib/caruby/domain/uniquify.rb
caruby-core-1.4.1 lib/caruby/domain/uniquify.rb