Skip to content

Commit

Permalink
Add Faker::Alphanumeric (faker-ruby#1302)
Browse files Browse the repository at this point in the history
* Add Faker::Alphanum

* Update and rename alphanum.md to alphanumeric.md

* Update README.md

* Remove duplications

* Minor change
  • Loading branch information
mtancoigne authored and vbrazo committed Sep 20, 2018
1 parent b8d0671 commit 14a86bf
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 48 deletions.
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 @@ -198,6 +198,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

0 comments on commit 14a86bf

Please sign in to comment.