class ActiveTriples::LocalName::Minter
Provide a standard interface for minting new IDs and validating the ID is not in use in any known (i.e., registered) repository.
Public Class Methods
Default minter used by generate_id.
@param [Hash] options the options to use while generating the local name @option options [String] :prefix the prefix to put before the generated local name (optional)
@note Because :prefix
is optional, errors in type for :prefix
are ignored. Any additional
parameters beyond <tt>:prefix</tt> are also ignored.
@note Best practice is to begin localnames with an alpha character. UUIDs can generate with an alpha or
numeric as the first character. Pass in an alpha character as <tt>:prefix</tt> to enforce this best practice.
@note See README for example overriding default minter.
@return [String] a uuid
# File lib/active_triples/local_name/minter.rb, line 78 def self.default_minter( *options ) prefix = options[0][:prefix] if ! options.empty? && options[0].is_a?(Hash) && options[0].key?(:prefix) local_name = SecureRandom.uuid local_name = prefix + local_name if prefix && prefix.is_a?(String) local_name end
Generate a random localname that combined with a class’ base_uri does not already exist in registered triplestores.
@param [Class] the class inheriting from ActiveTriples::Reource
(ActiveTriples
< 0.7) or including ActiveTriples::RDFSource
(ActiveTriples
>= 0.7) whose configuration
is used to generate the full URI for testing for uniqueness of the generated local name
@param [Integer] the maximum number of attempts to make a unique local name @yieldparam the arguments to pass to the minter block (optional) @yield the function to use to mint the local name. If not specified, the
+default_minter+ function will be used to generate an UUID.
@yieldreturn [String] the generated local name to be tested for uniqueness
@return [String] the generated local name
@raise [ArgumentError] if maximum allowed tries is less than 0 @raise [ArgumentError] if for_class does not inherit from ActiveTriples::Resources (ActiveTriples
< 0.7) or include ActiveTriples:RDFSource (ActiveTriples
>= 0.7) @raise [ArgumentError] if minter_block is not a block (does not respond to call) @raise [Exception] if for_class does not have base_uri configured @raise [Exception] if an available local name is not found in the maximum allowed tries.
@note See README for examples passing in minter block.
@TODO This is inefficient if max_tries is large. Could try
multi-threading. When using the default_minter included in this class, it is unlikely to be a problem and should find an ID within the first few attempts.
# File lib/active_triples/local_name/minter.rb, line 35 def self.generate_local_name(for_class, max_tries=10, *minter_args, &minter_block) raise ArgumentError, 'Argument max_tries must be >= 1 if passed in' if max_tries <= 0 if( ActiveTriples::LocalName.post_ActiveTriples_0_7? ) # Supports ActiveTriples >= 0.7 raise ArgumentError, 'Argument for_class must inherit from ActiveTriples::Resource or include module ActiveTriples::RDFSource' unless ( for_class < ActiveTriples::Resource || for_class.included_modules.include?(ActiveTriples::RDFSource) ) else # Supports ActiveTriples < 0.7 raise ArgumentError, 'Argument for_class must inherit from ActiveTriples::Resource' unless ( for_class < ActiveTriples::Resource ) end raise 'Requires base_uri to be defined in for_class.' unless for_class.base_uri raise ArgumentError, 'Invalid minter_block.' if minter_block && !minter_block.respond_to?(:call) minter_block = proc { |args| default_minter(args) } unless minter_block found = true test_id = nil (1).upto(max_tries) do test_id = minter_block.call( *minter_args ) found = for_class.id_persisted?(test_id) break unless found end raise 'Available ID not found. Exceeded maximum tries.' if found test_id end