File: base.rb

package info (click to toggle)
ruby-model-tokenizer 1.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 180 kB
  • sloc: ruby: 245; makefile: 2
file content (42 lines) | stat: -rw-r--r-- 1,163 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
module ModelTokenizer
  module Base
    CHARSET = %w{
      a b c d e f g h i j k   m n o p q r s t u v w x y z
      A B C D E F G H   J K L M N   P   R S T     W X Y Z
      2 3 4 5 6 7 8 9
      - _
    }

    #Default length is 14 characters. Provides 
    #56!/(14!*(56-14)!) = 5,804,731,963,800 unique tokens.
    @@model_tokenizer_token_length = 14

    def model_tokenizer_token_length
      @@model_tokenizer_token_length
    end

    def has_token(*attributes)
      options = {
        :length => @@model_tokenizer_token_length
      }.merge!(attributes.last.is_a?(Hash) ? attributes.pop : {})

      if(!options[:length].is_a?(Integer) || options[:length] < 8)
        options[:length] = @@model_tokenizer_token_length
      end

      @@model_tokenizer_token_length = options[:length]

      include InstanceMethods
    end

    module InstanceMethods
      protected
      def generate_token
        self.token = loop do
          random_token = (0...self.class.model_tokenizer_token_length).map{CHARSET[rand(CHARSET.size)]}.join
          break random_token unless self.class.exists?(:token => random_token)
        end
      end
    end
  end
end