module Jafry::Identificator
Module for automaticaly creatings schemes ids @since 0.2.0
Public Class Methods
Creates new scheme id type
@param type [String] Id type @return [Type] Type of id
# File lib/jafry/identificator.rb, line 62 def build_type(type) const_get("Jafry::Identificator::#{type.capitalize}").new end
Finds registered scheme and if there is no one, creates new
@param scheme_name [String] Scheme
name @return [Hash] Identification information about scheme
# File lib/jafry/identificator.rb, line 43 def find_or_register(scheme_name) return find_scheme(scheme_name) if find_scheme(scheme_name) register_scheme(scheme_name) end
Finds registered scheme
@param scheme_name [String] Scheme
name @return [Hash] Identification information about scheme
# File lib/jafry/identificator.rb, line 34 def find_scheme(scheme_name) self.schemes.select {|item| item[:scheme] == scheme_name}.last end
Helper method for generate unique hash
@return [String] Unique md5 hash
# File lib/jafry/identificator.rb, line 95 def generate_hash Digest::MD5.hexdigest([Time.now, rand].join) end
Generates id
@param [Scheme] scheme Scheme
instance
# File lib/jafry/identificator.rb, line 70 def generate_id(scheme) config = Jafry::Configurator.get_config(scheme.self_name) build_type(config[:id_type]).id_scaffold(config[:id_wrapper], scheme) end
Creates hash id
@param [Scheme] scheme Scheme
instance
# File lib/jafry/identificator.rb, line 87 def hash_counter(scheme) find_or_register(scheme.self_name)[:counter] = generate_hash end
Increment id counter by 1
@param [Scheme] scheme Scheme
instance
# File lib/jafry/identificator.rb, line 79 def inc_scheme_counter(scheme) find_or_register(scheme.self_name)[:counter] += 1 end
Creates identification record about scheme
@param scheme_name [String] Scheme
name @return [Hash] Identification information about scheme
# File lib/jafry/identificator.rb, line 21 def register_scheme(scheme_name) config = Jafry::Configurator.get_config(scheme_name) config = Jafry::Configurator.set_config(scheme_name) unless config scheme = build_type(config[:id_type]).scheme_scaffold(scheme_name) self.schemes << scheme scheme end