Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add Faker::Alphanumeric #1302

Merged
merged 5 commits into from
Sep 20, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Contents
- [Installing](#installing)
- [Usage](#usage)
- [Faker::Address](doc/address.md)
- [Faker::Alphanumeric](doc/alphanumeric.md)
- [Faker::Ancient](doc/ancient.md)
- [Faker::App](doc/app.md)
- [Faker::Appliance](doc/appliance.md)
Expand Down
9 changes: 9 additions & 0 deletions doc/alphanumeric.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Faker::Alphanumeric

It might be available in the next version.

```ruby
Faker::Alphanumeric.alpha 10 #=> "zlvubkrwga"

Faker::Alphanumeric.alphanumeric 10 #=> "3yfq2phxtb"
```
10 changes: 10 additions & 0 deletions lib/faker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ def rand_in_range(from, to)
rand(from..to)
end

# If an array or range is passed, a random value will be selected.
# All other values are simply returned.
def resolve(value)
case value
when Array then sample(value)
when Range then rand value
else value
end
end

def unique(max_retries = 10_000)
@unique ||= UniqueGenerator.new(self, max_retries)
end
Expand Down
22 changes: 22 additions & 0 deletions lib/faker/alphanumeric.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Faker
class Alphanumeric < Base
class << self
ALPHABET = ('a'..'z').to_a
ALPHANUMS = ALPHABET + (0..9).to_a

def alpha(char_count = 32)
char_count = resolve(char_count)
return '' if char_count.to_i < 1
Array.new(char_count) { sample(ALPHABET) }.join
end

def alphanumeric(char_count = 32)
char_count = resolve(char_count)
return '' if char_count.to_i < 1
Array.new(char_count) { sample(ALPHANUMS) }.join
end
end
end
end
12 changes: 0 additions & 12 deletions lib/faker/hipster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,6 @@ def paragraph_by_chars(chars = 256, supplemental = false)

paragraph[0...chars - 1] + '.'
end

private

# If an array or range is passed, a random value will be selected.
# All other values are simply returned.
def resolve(value)
case value
when Array then value[rand(value.size)]
when Range then value.to_a[rand(value.size)]
else value
end
end
end
end
end
18 changes: 2 additions & 16 deletions lib/faker/lorem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
module Faker
# Based on Perl's Text::Lorem
class Lorem < Base
CHARACTERS = ('0'..'9').to_a + ('a'..'z').to_a

class << self
def word
sample(translate('faker.lorem.words'))
Expand All @@ -21,13 +19,11 @@ def words(num = 3, supplemental = false)
end

def character
sample(CHARACTERS)
sample(Types::CHARACTERS)
end

def characters(char_count = 255)
char_count = resolve(char_count)
return '' if char_count.to_i < 1
Array.new(char_count) { sample(CHARACTERS) }.join
Alphanumeric.alphanumeric(char_count)
end

def multibyte
Expand Down Expand Up @@ -79,16 +75,6 @@ def locale_space
def locale_question_mark
translate('faker.lorem.punctuation.question_mark') || '?'
end

# If an array or range is passed, a random value will be selected.
# All other values are simply returned.
def resolve(value)
case value
when Array then sample(value)
when Range then rand value
else value
end
end
end
end
end
12 changes: 0 additions & 12 deletions lib/faker/lovecraft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,6 @@ def paragraph_by_chars(chars = 256)

paragraph[0...chars - 1] + '.'
end

private

# If an array or range is passed, a random value will be selected.
# All other values are simply returned.
def resolve(value)
case value
when Array then sample(value)
when Range then rand value
else value
end
end
end
end
end
8 changes: 0 additions & 8 deletions lib/faker/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,6 @@ def random_complex_type
def titleize(word)
word.split(/(\W)/).map(&:capitalize).join
end

def resolve(value)
case value
when Array then sample(value)
when Range then rand value
else value
end
end
end
end
end
17 changes: 17 additions & 0 deletions test/test_alphanum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require_relative 'test_helper'

class TestFakerAlphanum < Test::Unit::TestCase
def setup
@tester = Faker::Alphanumeric
end

def alpha
assert @tester.alpha(5).match(/[a-z]{5}/)
end

def alphanum
assert @tester.alphanumeric(5).match(/[a-z0-9]{5}/)
end
end