Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,16 @@ passing an options hash. The following keys are supported:

`:length` number of characters, defaults to __32__

`:type` type of string, can be one of: `:human`, `:auto`, defaults to __:auto__
`:type` type of string, can be one of: `:human`, `:numbers`, or `:auto` and defaults to __:auto__

Human type generates strings easier to read by excluding ambiguous characters like `1, 5, 8, B, o, O, I, l, s, u`.

`:blacklist` characters to exclude when generating the random string, defaults to __[]__

`:scope` scopes, defines the `ActiveRecord` `scope` applied before calculating the `position` field value. Defaults to __[]__

`:chars` Custom character list. If provided then `:type` will be ignored. `:chars` can be a range, an array of characters, or a string of characters.

To generate a unique-random on the fly `Uniqueness.generate` that will produce a random field for you.

You can also pass some options `Uniqueness.generate(type: :human)`.
Expand Down
8 changes: 7 additions & 1 deletion lib/uniqueness/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
module Uniqueness
class << self
def generate(opts = {})
options ||= {}
options = uniqueness_default_options.merge(opts)
dict = uniqueness_dictionary - options[:blacklist]
dict -= [*(:A..:Z)].map(&:to_s) unless options[:case_sensitive]
dict -= uniqueness_ambigious_dictionary if options[:type].to_sym == :human
dict = uniqueness_numbers_dictionary if options[:type].to_sym == :numbers
dict = uniqueness_custom_dictionary(options[:chars]) if options[:chars].present?
code = Array.new(options[:length]).map { dict[SecureRandom.random_number(dict.length)] }.join
"#{options[:prefix]}#{code}#{options[:suffix]}"
end
Expand All @@ -26,6 +26,12 @@ def uniqueness_numbers_dictionary
[*(0..9)].map(&:to_s)
end

def uniqueness_custom_dictionary(chars = [])
chars = [*chars] if chars.is_a?(Range)
chars = chars.map(&:to_s) if chars.is_a?(Array)
chars
end

# Default sorting options
def uniqueness_default_options
{
Expand Down
2 changes: 1 addition & 1 deletion lib/uniqueness/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Uniqueness
VERSION = '1.1.0'.freeze
VERSION = '1.1.1'.freeze
end
3 changes: 3 additions & 0 deletions spec/generator/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
it { expect(Uniqueness.generate(type: :numbers)).to match(/^[0-9]+$/) }
it { expect(Uniqueness.generate(prefix: 'hassan').index('hassan')).to eq 0 }
it { expect(Uniqueness.generate(suffix: 'hassan')).to end_with "hassan" }
it { expect(Uniqueness.generate(chars: 'A'..'F')).to match(/^[A-F]+$/) }
it { expect(Uniqueness.generate(chars: ('A'..'F').to_a)).to match(/^[A-F]+$/) }
it { expect(Uniqueness.generate(chars: 'ABCDEF')).to match(/^[A-F]+$/) }
end
end
2 changes: 1 addition & 1 deletion spec/uniqueness/uniqueness_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
end
end

context 'intialized new object' do
context 'initialized new object' do
it { expect(new_page.after_init_token).not_to be_nil }

context 'length' do
Expand Down