unique_names_generator 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/Gemfile.lock +1 -1
- data/README.md +1 -1
- data/lib/unique_names_generator/version.rb +1 -1
- data/lib/unique_names_generator.rb +109 -126
- data/unique_names_generator.gemspec +2 -1
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 65b327e97d35d0196016feff3a0f7814f4b398405c58a6a398beb835a364a74d
|
4
|
+
data.tar.gz: ad729f68a508030690fcf38c56bd899bce70e372886a113995ef63ca8e896751
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '038bb93ba4b7bc097fe528575bc6ba4debb7810b8f2c0d204cdd2a08bfaf82cd888ee7972afb14ed34d09f7fecde9f62b3e0b7ef18246720f67b59e24e69fcb4'
|
7
|
+
data.tar.gz: 4c4aa71be2f0f243ebf30f1184d41842c2627380271428463c53120bf2676e49d6539f492b948d5908d99e3c1be3f9f6e029fe05f4118e7a05e014777edecedb
|
data/.gitignore
ADDED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -30,163 +30,146 @@ module UniqueNamesGenerator
|
|
30
30
|
generate_name(dictionaries)
|
31
31
|
end
|
32
32
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
dictionary
|
38
|
-
|
39
|
-
|
33
|
+
class << self
|
34
|
+
private
|
35
|
+
|
36
|
+
def match_word_list(dictionary)
|
37
|
+
module_name = camelize_dictionary(dictionary)
|
38
|
+
begin
|
39
|
+
dictionary = Dictionaries.const_get(module_name)
|
40
|
+
dictionary.list_all
|
41
|
+
rescue NameError
|
42
|
+
raise_invalid_dictionary(dictionary)
|
43
|
+
end
|
40
44
|
end
|
41
|
-
end
|
42
45
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
46
|
+
def word_list(dictionary)
|
47
|
+
case dictionary
|
48
|
+
when Array
|
49
|
+
dictionary if dictionary.all? { |item| item.is_a?(String) }
|
50
|
+
when Symbol
|
51
|
+
match_word_list(dictionary)
|
52
|
+
else
|
53
|
+
raise ArgumentError, 'Dictionary contains invalid dictionary type'
|
54
|
+
end
|
51
55
|
end
|
52
|
-
end
|
53
56
|
|
54
|
-
|
55
|
-
|
57
|
+
def random_seeded_float
|
58
|
+
seed_value = Seed.generate_seed(@seed)
|
56
59
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
def map_dictionaries(dictionaries)
|
62
|
-
dictionaries.map { |dictionary| word_list(dictionary) }
|
63
|
-
end
|
60
|
+
prng = @seed.nil? ? Random.new : Random.new(seed_value)
|
61
|
+
prng.rand
|
62
|
+
end
|
64
63
|
|
65
|
-
|
66
|
-
|
67
|
-
word.split(/(?=[A-Z])/)
|
68
|
-
else
|
69
|
-
word.split(@separator)
|
64
|
+
def map_dictionaries(dictionaries)
|
65
|
+
dictionaries.map { |dictionary| word_list(dictionary) }
|
70
66
|
end
|
71
|
-
end
|
72
67
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
# Preserve original capitalization for parts with non-letter characters
|
77
|
-
part
|
68
|
+
def split_with_separator(word)
|
69
|
+
if @separator.nil?
|
70
|
+
word.split(/(?=[A-Z])/)
|
78
71
|
else
|
79
|
-
|
72
|
+
word.split(@separator)
|
80
73
|
end
|
81
|
-
end
|
82
|
-
end
|
74
|
+
end
|
83
75
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
76
|
+
def format_parts(parts)
|
77
|
+
parts.map do |part|
|
78
|
+
if part.match?(/[^a-zA-Z]/) && @style == :capital
|
79
|
+
# Preserve original capitalization for parts with non-letter characters
|
80
|
+
part
|
81
|
+
else
|
82
|
+
format_word(part, @style)
|
83
|
+
end
|
84
|
+
end.join(@separator)
|
85
|
+
end
|
86
|
+
|
87
|
+
def format_multi_word(word, original_word)
|
88
|
+
if original_word.include?(' ') || original_word.include?('-')
|
89
|
+
parts = split_with_separator(word)
|
90
|
+
format_parts(parts)
|
91
|
+
else
|
92
|
+
format_word(word, @style)
|
93
|
+
end
|
90
94
|
end
|
91
|
-
end
|
92
95
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
96
|
+
def generate_name(dictionaries)
|
97
|
+
if @creativity.nil? || @creativity.zero?
|
98
|
+
generate_name_original(dictionaries)
|
99
|
+
else
|
100
|
+
generate_name_creatively(dictionaries)
|
101
|
+
end
|
98
102
|
end
|
99
|
-
end
|
100
103
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
104
|
+
def generate_name_original(dictionaries)
|
105
|
+
map_dictionaries(dictionaries).reduce(nil) do |acc, x|
|
106
|
+
rnd = (random_seeded_float * x.length).floor
|
107
|
+
original_word = x[rnd]
|
105
108
|
|
106
|
-
|
109
|
+
output_word(acc, original_word)
|
110
|
+
end
|
107
111
|
end
|
108
|
-
end
|
109
112
|
|
110
|
-
|
111
|
-
|
113
|
+
def generate_name_creatively(dictionaries)
|
114
|
+
word_lists = map_dictionaries(dictionaries)
|
112
115
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
116
|
+
word_lists.each_with_index.reduce(nil) do |acc, (word_list, index)|
|
117
|
+
creativity = calculate_creativity(index)
|
118
|
+
rnd = (random_seeded_float * word_list.length * creativity).floor
|
119
|
+
original_word = word_list[rnd % word_list.length] # Ensure we don't go out of bounds
|
117
120
|
|
118
|
-
|
121
|
+
output_word(acc, original_word)
|
122
|
+
end
|
119
123
|
end
|
120
|
-
end
|
121
124
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
+
def output_word(acc, original_word)
|
126
|
+
word = format_with_separator(original_word)
|
127
|
+
word = format_multi_word(word, original_word)
|
125
128
|
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
129
|
+
if acc
|
130
|
+
"#{acc}#{@separator}#{word}"
|
131
|
+
else
|
132
|
+
word
|
133
|
+
end
|
130
134
|
end
|
131
|
-
end
|
132
135
|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
136
|
+
def calculate_creativity(index)
|
137
|
+
if index.zero?
|
138
|
+
@creativity # Base creativity for the first dictionary
|
139
|
+
else
|
140
|
+
@creativity * (2 + index * 0.5) # Increase creativity for subsequent dictionaries
|
141
|
+
end
|
138
142
|
end
|
139
|
-
end
|
140
143
|
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
+
def camelize_dictionary(dictionary)
|
145
|
+
dictionary.to_s.split('_').map(&:capitalize).join
|
146
|
+
end
|
144
147
|
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
+
def raise_invalid_dictionary(dictionary)
|
149
|
+
raise ArgumentError, "Invalid dictionary: #{dictionary}"
|
150
|
+
end
|
148
151
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
152
|
+
def format_with_separator(word)
|
153
|
+
if @separator.nil?
|
154
|
+
# If separator is empty, just remove spaces without changing case
|
155
|
+
word.gsub(/\s+/, '')
|
156
|
+
else
|
157
|
+
# If there's a separator, use it to replace spaces
|
158
|
+
word.gsub(/\s+/, @separator)
|
159
|
+
end
|
156
160
|
end
|
157
|
-
end
|
158
161
|
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
162
|
+
def format_word(word, style)
|
163
|
+
case style
|
164
|
+
when :lowercase
|
165
|
+
word.downcase
|
166
|
+
when :uppercase
|
167
|
+
word.upcase
|
168
|
+
when :capital
|
169
|
+
word.capitalize
|
170
|
+
else
|
171
|
+
word
|
172
|
+
end
|
169
173
|
end
|
170
174
|
end
|
171
|
-
|
172
|
-
private_class_method(
|
173
|
-
*%i[
|
174
|
-
generate_name
|
175
|
-
match_word_list
|
176
|
-
word_list
|
177
|
-
random_seeded_float
|
178
|
-
map_dictionaries
|
179
|
-
camelize_dictionary
|
180
|
-
raise_invalid_dictionary
|
181
|
-
format_with_separator
|
182
|
-
format_multi_word
|
183
|
-
format_parts
|
184
|
-
format_word
|
185
|
-
split_with_separator
|
186
|
-
generate_name_original
|
187
|
-
generate_name_creatively
|
188
|
-
calculate_creativity
|
189
|
-
output_word
|
190
|
-
]
|
191
|
-
)
|
192
175
|
end
|
@@ -15,7 +15,8 @@ Gem::Specification.new do |s|
|
|
15
15
|
s.email = 'jongirard03@gmail.com'
|
16
16
|
s.homepage = 'https://github.com/jongirard/unique_names_generator_ruby'
|
17
17
|
s.metadata = { 'homepage_uri' => 'https://github.com/jongirard/unique_names_generator_ruby',
|
18
|
-
'source_code_uri' => 'https://github.com/jongirard/unique_names_generator_ruby'
|
18
|
+
'source_code_uri' => 'https://github.com/jongirard/unique_names_generator_ruby',
|
19
|
+
'documentation_uri' => 'https://jongirard.github.io/unique_names_generator_ruby' }
|
19
20
|
s.require_paths = ['lib']
|
20
21
|
s.license = 'MIT'
|
21
22
|
s.files = Dir.chdir(File.expand_path(__dir__)) do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unique_names_generator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jon Girard
|
@@ -34,6 +34,7 @@ extensions: []
|
|
34
34
|
extra_rdoc_files: []
|
35
35
|
files:
|
36
36
|
- ".github/workflows/ruby.yml"
|
37
|
+
- ".gitignore"
|
37
38
|
- ".pryrc"
|
38
39
|
- ".rspec"
|
39
40
|
- ".rubocop.yml"
|
@@ -59,6 +60,7 @@ licenses:
|
|
59
60
|
metadata:
|
60
61
|
homepage_uri: https://github.com/jongirard/unique_names_generator_ruby
|
61
62
|
source_code_uri: https://github.com/jongirard/unique_names_generator_ruby
|
63
|
+
documentation_uri: https://jongirard.github.io/unique_names_generator_ruby
|
62
64
|
post_install_message:
|
63
65
|
rdoc_options: []
|
64
66
|
require_paths:
|