token_phrase 1.0.3 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +19 -2
- data/lib/token_phrase/version.rb +1 -1
- data/lib/token_phrase.rb +11 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -37,6 +37,8 @@ TokenPhrase.generate
|
|
37
37
|
"sweet-jade-cracked-cape975328"
|
38
38
|
```
|
39
39
|
|
40
|
+
With the current dictionaries and numbers, there are 4,199,040,000,000 (four trillion!) unique possibilites.
|
41
|
+
|
40
42
|
### Separators
|
41
43
|
If you would like a different separator, just pass a string as an argument:
|
42
44
|
|
@@ -91,7 +93,7 @@ And you can, of course pass a separator before the dictionaries:
|
|
91
93
|
"awesome^^^ultraviolet^^^striped^^^hat748152"
|
92
94
|
```
|
93
95
|
|
94
|
-
|
96
|
+
### Numbers
|
95
97
|
|
96
98
|
To help with uniqueness, a random number is added to the token by default. This may not be your cup of tea, so if you pass :numbers => false with the dictionaries, you can remove the number:
|
97
99
|
|
@@ -117,7 +119,22 @@ TokenPhrase.generate :patterns => your_patterns
|
|
117
119
|
=> "awesome-mauve-magic-eye-giraffe"
|
118
120
|
```
|
119
121
|
|
120
|
-
##
|
122
|
+
## TokenPhrase.permutations(dictionaries = {})
|
123
|
+
|
124
|
+
If you want to see how many unique combinations you can generate, use the permutations method. Just pass it the dictionaries hash the same way you would use TokenPhrase.generate:
|
125
|
+
|
126
|
+
```ruby
|
127
|
+
TokenPhrase.permutations
|
128
|
+
=> 4199040000000
|
129
|
+
|
130
|
+
TokenPhrase.permutations :nouns => %w(cat dog)
|
131
|
+
=> 87480000000
|
132
|
+
|
133
|
+
TokenPhrase.permutations :nouns => %w(scooter boat car vroom-vroom), :numbers => false
|
134
|
+
=> 174960
|
135
|
+
```
|
136
|
+
|
137
|
+
## Rails Uniqueness
|
121
138
|
|
122
139
|
The simplest way to create a unique token for your models is to add a before_create filter to your model:
|
123
140
|
|
data/lib/token_phrase/version.rb
CHANGED
data/lib/token_phrase.rb
CHANGED
@@ -5,6 +5,7 @@ module TokenPhrase
|
|
5
5
|
Colors = %w(red yellow blue green violet taupe mauve lime golden silver grey black white tangello sunshine brown tan infrared ultraviolet pink beige almond aquamarine burnt-orange cerulean cornflower-blue denim forest-green midnight-blue peach plum sea-green ruby emerald jade rose topaz onyx pearl coral crimson cyan chocolate aqua azure lavendar chiffon khaki ivory magenta navy-blue olive salmon turquoise)
|
6
6
|
Patterns = %w(striped checked spotted polka-dotted plaid wavy houndstooth argyle glossy matte pinstriped tartan paisley satin honeycomb fractal waved cracked )
|
7
7
|
Nouns = %w(floutist carpenter jacket president address machine computer mug lamp phone wall bicycle river lake fountain building book hat pants shirt cape soup gloves pen suit photograph sand profit energy fork compact-disk floppy-disk chandelier door window laboratory people tapir wolverine wolf spider wolf-spider spider-wolf banana-slug giraffe deer-mouse capybara dingo dragon cardinal owl octopus elk moose weasel elephant rhino iguana bullfrog greyhound stickbug ladybug ant rat coyote chimpanzee housecat barracuda raven crane fox panda racoon nessie whale dolphin shark viper frog toad flounder skunk wookie dishwasher bat space-heater bobble-head lego-set pinboard flag tv video-game envelope headphones mousepad jukebox)
|
8
|
+
Numbers = [*1..100]
|
8
9
|
|
9
10
|
def self.adjectives(more_adjectives = nil)
|
10
11
|
more_adjectives.nil? ? TokenPhrase::Adjectives : TokenPhrase::Adjectives | more_adjectives
|
@@ -32,8 +33,17 @@ module TokenPhrase
|
|
32
33
|
dictionaries[:patterns] ||= TokenPhrase::Patterns
|
33
34
|
dictionaries[:nouns] ||= TokenPhrase::Nouns
|
34
35
|
phrase = [dictionaries[:adjectives].sample, dictionaries[:colors].sample, dictionaries[:patterns].sample, dictionaries[:nouns].sample].join('-')
|
35
|
-
phrase <<
|
36
|
+
phrase << Random.new.rand(1..1000000).to_s unless dictionaries[:numbers] == false
|
36
37
|
phrase.gsub!(/-/, separator) unless separator.nil?
|
37
38
|
return phrase
|
38
39
|
end
|
40
|
+
|
41
|
+
def self.permutations(dictionaries = {})
|
42
|
+
dictionaries[:adjectives] ||= TokenPhrase::Adjectives
|
43
|
+
dictionaries[:colors] ||= TokenPhrase::Colors
|
44
|
+
dictionaries[:patterns] ||= TokenPhrase::Patterns
|
45
|
+
dictionaries[:nouns] ||= TokenPhrase::Nouns
|
46
|
+
permutations = [:adjectives, :colors, :patterns, :nouns].map{ |key| dictionaries[key].uniq.count }.inject{ |product, total| product * total }
|
47
|
+
dictionaries[:numbers] == false ? permutations : permutations * 1000000
|
48
|
+
end
|
39
49
|
end
|