factory-helper 1.6.7 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 72a61bdc1de1cbd5de1abd330d48567051083ffb
4
- data.tar.gz: ecf403f1b7acca0beb50a523e73f34f8aca86c76
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NjVhZTJiYjU2NDQ5YmIzY2EwOTZlYmI0Zjc1NzljZjhhZTZlODY0NA==
5
+ data.tar.gz: !binary |-
6
+ OGE5NmYyOTBhODJhZGU4NDcyYWYyNzE3ZDY1NzA4OWQ3Mzc2NDcyNA==
5
7
  SHA512:
6
- metadata.gz: 96e8d7ddf078379852f401de34632b79d0ecc02a554c1621d0fed7ec27baca848ed7f54a458128bdefb04067095469eacc2e6321f3c62df255e3f9530fd785a6
7
- data.tar.gz: 96b16daa17f269addcd12f1bdc24b3776c624fd57cbfc9742dff574926f1e6d83d1959d66a664c15920938bf3cdb77b5ce535adbdbaa02532a597b763f18debb
8
+ metadata.gz: !binary |-
9
+ YzVlNDIxZDZiZTgxMWVmNmQ1MDdhYjkzODI4OTRiMDU3ODY3ZmZmZjY3ZDMx
10
+ MDc5NWNjY2EyNmU1ODkyZGM0ZjliY2VjNTg4MGFhZjA0MDQ4ODY2OTM3MGYz
11
+ MzY3OWE2NjQ5NzZmN2Y3ZWM5MzI0ZjJiZGNmNjY1MGY2NTMzYzI=
12
+ data.tar.gz: !binary |-
13
+ YmNhN2VkYWI0ZDViMGE1ODIzYTcxNzQxM2NiMTRlNjllZTI5NzZkYTVlZDU5
14
+ NmZmNzA1NTdmODM5YzdhODA2MGEwYzZjNTJkNjYzMDM5MTk5YzI2MjkwYzMw
15
+ MDdkY2NkN2I2MzcwN2QyMTEzYTgyM2YzMTBmOGM5MmM3YTY3OTI=
data/README.md CHANGED
@@ -35,9 +35,29 @@ FactoryHelper::Name.name #=> "Christophe Bartell"
35
35
  FactoryHelper::Internet.email #=> "kirsten.greenholt@corkeryfisher.info"
36
36
  ```
37
37
 
38
+ ###Seeding
39
+ ----------
40
+ FactoryHelper now supports seeding of Ruby's pseudo-random number generator
41
+ (PRNG) to provide deterministic output of repeated method calls.
42
+
43
+ ```ruby
44
+ FactoryHelper::Config.seed = 99
45
+ FactoryHelper::Company.bs #=> "utilize real-time functionalities"
46
+ FactoryHelper::Company.bs #=> "orchestrate wireless web-readiness"
47
+
48
+ FactoryHelper::Config.seed = 99
49
+ FactoryHelper::Company.bs #=> "utilize real-time functionalities"
50
+ FactoryHelper::Company.bs #=> "orchestrate wireless web-readiness"
51
+
52
+ FactoryHelper::Config.seed = nil # seeds the PRNG with Ruby's default entropy source
53
+
54
+ FactoryHelper::Config.random.seed #=> 57010835277627224902731113142647237214
55
+
56
+ FactoryHelper::Company.bs #=> "revolutionize plug-and-play architectures"
57
+ ```
58
+
38
59
  ###FactoryHelper::String
39
60
  -----------------
40
-
41
61
  Random UTF-8 string with an optional length argument
42
62
 
43
63
  ```ruby
@@ -297,9 +317,11 @@ FactoryHelper::Avatar.image("my-own-slug", "50x50", "bmp") #=> "http://robohash.
297
317
  ----------------
298
318
 
299
319
  ```ruby
300
- # Required parameter: digits
320
+ # Optional parameter: digits
301
321
  FactoryHelper::Number.number(10) #=> "1968353479"
302
322
 
323
+ FactoryHelper::Number.hexadecimal(10) #=> "9b7b26aa4c"
324
+
303
325
  FactoryHelper::Number.digit #=> "1"
304
326
  ```
305
327
 
@@ -404,7 +426,6 @@ FactoryHelper::App.author #=> "Daphne Swift"
404
426
 
405
427
  ###FactoryHelper::SlackEmoji
406
428
  -----------------
407
-
408
429
  Random Slack Emoji
409
430
 
410
431
  ```ruby
@@ -468,7 +489,7 @@ See [CONTRIBUTING.md](https://github.com/factory-helper/factory-helper/blob/mast
468
489
 
469
490
  Contact
470
491
  -------
471
- Comments and feedback are welcome. Send an email to factory-helper@googlegroups.com.
492
+ Comments and feedback are welcome via GitHub Issues.
472
493
 
473
494
  License
474
495
  -------
@@ -17,12 +17,19 @@ I18n.load_path += Dir[File.join(mydir, 'locales', '*.yml')]
17
17
  module FactoryHelper
18
18
  class Config
19
19
  @locale = nil
20
+ @random = Random.new
20
21
 
21
22
  class << self
22
23
  attr_writer :locale
24
+ attr_reader :random
25
+
23
26
  def locale
24
27
  @locale || I18n.locale
25
28
  end
29
+
30
+ def seed=(seed)
31
+ @random = seed ? Random.new(seed) : Random.new
32
+ end
26
33
  end
27
34
  end
28
35
 
@@ -34,11 +41,11 @@ module FactoryHelper
34
41
  class << self
35
42
  ## make sure numerify results doesn’t start with a zero
36
43
  def numerify(number_string)
37
- number_string.sub(/#/) { (rand(9)+1).to_s }.gsub(/#/) { rand(10).to_s }
44
+ number_string.sub(/#/) { (FactoryHelper::Config.random.rand(9)+1).to_s }.gsub(/#/) { FactoryHelper::Config.random.rand(10).to_s }
38
45
  end
39
46
 
40
47
  def letterify(letter_string)
41
- letter_string.gsub(/\?/) { ULetters.sample }
48
+ letter_string.gsub(/\?/) { ULetters.sample(:random => FactoryHelper::Config.random) }
42
49
  end
43
50
 
44
51
  def bothify(string)
@@ -69,21 +76,21 @@ module FactoryHelper
69
76
  re.
70
77
  gsub(/^\/?\^?/, '').gsub(/\$?\/?$/, ''). # Ditch the anchors
71
78
  gsub(/\{(\d+)\}/, '{\1,\1}').gsub(/\?/, '{0,1}'). # All {2} become {2,2} and ? become {0,1}
72
- gsub(/(\[[^\]]+\])\{(\d+),(\d+)\}/) {|match| $1 * Array(Range.new($2.to_i, $3.to_i)).sample }. # [12]{1,2} becomes [12] or [12][12]
73
- gsub(/(\([^\)]+\))\{(\d+),(\d+)\}/) {|match| $1 * Array(Range.new($2.to_i, $3.to_i)).sample }. # (12|34){1,2} becomes (12|34) or (12|34)(12|34)
74
- gsub(/(\\?.)\{(\d+),(\d+)\}/) {|match| $1 * Array(Range.new($2.to_i, $3.to_i)).sample }. # A{1,2} becomes A or AA or \d{3} becomes \d\d\d
75
- gsub(/\((.*?)\)/) {|match| match.gsub(/[\(\)]/, '').split('|').sample }. # (this|that) becomes 'this' or 'that'
76
- gsub(/\[([^\]]+)\]/) {|match| match.gsub(/(\w\-\w)/) {|range| Array(Range.new(*range.split('-'))).sample } }. # All A-Z inside of [] become C (or X, or whatever)
77
- gsub(/\[([^\]]+)\]/) {|match| $1.split('').sample }. # All [ABC] become B (or A or C)
78
- gsub('\d') {|match| Numbers.sample }.
79
- gsub('\w') {|match| Letters.sample }
79
+ gsub(/(\[[^\]]+\])\{(\d+),(\d+)\}/) {|match| $1 * Array(Range.new($2.to_i, $3.to_i)).sample(:random => FactoryHelper::Config.random) }. # [12]{1,2} becomes [12] or [12][12]
80
+ gsub(/(\([^\)]+\))\{(\d+),(\d+)\}/) {|match| $1 * Array(Range.new($2.to_i, $3.to_i)).sample(:random => FactoryHelper::Config.random) }. # (12|34){1,2} becomes (12|34) or (12|34)(12|34)
81
+ gsub(/(\\?.)\{(\d+),(\d+)\}/) {|match| $1 * Array(Range.new($2.to_i, $3.to_i)).sample(:random => FactoryHelper::Config.random) }. # A{1,2} becomes A or AA or \d{3} becomes \d\d\d
82
+ gsub(/\((.*?)\)/) {|match| match.gsub(/[\(\)]/, '').split('|').sample(:random => FactoryHelper::Config.random) }. # (this|that) becomes 'this' or 'that'
83
+ gsub(/\[([^\]]+)\]/) {|match| match.gsub(/(\w\-\w)/) {|range| Array(Range.new(*range.split('-'))).sample(:random => FactoryHelper::Config.random) } }. # All A-Z inside of [] become C (or X, or whatever)
84
+ gsub(/\[([^\]]+)\]/) {|match| $1.split('').sample(:random => FactoryHelper::Config.random) }. # All [ABC] become B (or A or C)
85
+ gsub('\d') {|match| Numbers.sample(:random => FactoryHelper::Config.random) }.
86
+ gsub('\w') {|match| Letters.sample(:random => FactoryHelper::Config.random) }
80
87
  end
81
88
 
82
89
  # Helper for the common approach of grabbing a translation
83
90
  # with an array of values and selecting one of them.
84
91
  def fetch(key)
85
92
  fetched = translate("factory_helper.#{key}")
86
- fetched = fetched.sample if fetched.respond_to?(:sample)
93
+ fetched = fetched.sample(:random => FactoryHelper::Config.random) if fetched.respond_to?(:sample)
87
94
  if fetched.match(/^\//) and fetched.match(/\/$/) # A regex
88
95
  regexify(fetched)
89
96
  else
@@ -141,7 +148,7 @@ module FactoryHelper
141
148
 
142
149
  # Use the alternate form of translate to get a nil rather than a "missing translation" string
143
150
  if translation = translate(:factory_helper)[@flexible_key][m]
144
- translation.respond_to?(:sample) ? translation.sample : translation
151
+ translation.respond_to?(:sample) ? translation.sample(:random => FactoryHelper::Config.random) : translation
145
152
  else
146
153
  super
147
154
  end
@@ -150,7 +157,7 @@ module FactoryHelper
150
157
  # Generates a random value between the interval
151
158
  def rand_in_range(from, to)
152
159
  from, to = to, from if to < from
153
- Random.new.rand(from..to)
160
+ FactoryHelper::Config.random.rand(from..to)
154
161
  end
155
162
  end
156
163
  end
@@ -47,11 +47,11 @@ module FactoryHelper
47
47
  def country_code; fetch('address.country_code'); end
48
48
 
49
49
  def latitude
50
- ((rand * 180) - 90).to_s
50
+ ((FactoryHelper::Config.random.rand * 180) - 90).to_s
51
51
  end
52
52
 
53
53
  def longitude
54
- ((rand * 360) - 180).to_s
54
+ ((FactoryHelper::Config.random.rand * 360) - 180).to_s
55
55
  end
56
56
 
57
57
  end
@@ -18,7 +18,7 @@ module FactoryHelper
18
18
  address_for(:testnet)
19
19
  end
20
20
 
21
- protected
21
+ private
22
22
 
23
23
  def base58(str)
24
24
  alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
@@ -38,8 +38,9 @@ module FactoryHelper
38
38
  end
39
39
 
40
40
  def address_for(network)
41
+ hex_160_bit = 0xffffffffffffffffffffffffffffffffffffffff
41
42
  version = PROTOCOL_VERSIONS.fetch(network)
42
- hash = SecureRandom.hex(20)
43
+ hash = FactoryHelper::Config.random.rand(hex_160_bit).to_s(16)
43
44
  packed = version.chr + [hash].pack("H*")
44
45
  checksum = Digest::SHA2.digest(Digest::SHA2.digest(packed))[0..3]
45
46
  base58(packed + checksum)
@@ -8,7 +8,7 @@ module FactoryHelper
8
8
 
9
9
  def department(max = 3, fixed_amount = false)
10
10
  num = max if fixed_amount
11
- num ||= 1 + rand(max)
11
+ num ||= 1 + FactoryHelper::Config.random.rand(max)
12
12
 
13
13
  categories = categories(num)
14
14
 
@@ -28,7 +28,7 @@ module FactoryHelper
28
28
  end
29
29
 
30
30
  def price
31
- random = Random.new
31
+ random = FactoryHelper::Config.random
32
32
  (random.rand(0..100.0) * 100).floor/100.0
33
33
  end
34
34
 
@@ -13,29 +13,29 @@ module FactoryHelper
13
13
 
14
14
  # Generate a buzzword-laden catch phrase.
15
15
  def catch_phrase
16
- translate('factory_helper.company.buzzwords').collect {|list| list.sample }.join(' ')
16
+ translate('factory_helper.company.buzzwords').collect {|list| list.sample(:random => FactoryHelper::Config.random) }.join(' ')
17
17
  end
18
18
 
19
19
  def buzzword
20
- translate('factory_helper.company.buzzwords').flatten.sample
20
+ translate('factory_helper.company.buzzwords').flatten.sample(:random => FactoryHelper::Config.random)
21
21
  end
22
22
 
23
23
  # When a straight answer won't do, BS to the rescue!
24
24
  def bs
25
- translate('factory_helper.company.bs').collect {|list| list.sample }.join(' ')
25
+ translate('factory_helper.company.bs').collect {|list| list.sample(:random => FactoryHelper::Config.random) }.join(' ')
26
26
  end
27
27
 
28
28
  def ein
29
- ('%09d' % rand(10 ** 9)).gsub(/(\d\d)(\d\d\d\d\d\d\d)/, '\\1-\\2')
29
+ ('%09d' % FactoryHelper::Config.random.rand(10 ** 9)).gsub(/(\d\d)(\d\d\d\d\d\d\d)/, '\\1-\\2')
30
30
  end
31
31
 
32
32
  def duns_number
33
- ('%09d' % rand(10 ** 9)).gsub(/(\d\d)(\d\d\d)(\d\d\d\d)/, '\\1-\\2-\\3')
33
+ ('%09d' % FactoryHelper::Config.random.rand(10 ** 9)).gsub(/(\d\d)(\d\d\d)(\d\d\d\d)/, '\\1-\\2-\\3')
34
34
  end
35
35
 
36
36
  # Get a random company logo url in PNG format.
37
37
  def logo
38
- rand_num = Random.rand(13) + 1
38
+ rand_num = FactoryHelper::Config.random.rand(13) + 1
39
39
  "http://pigment.github.io/fake-logos/logos/medium/color/#{rand_num}.png"
40
40
  end
41
41
  end
@@ -3,7 +3,7 @@ module FactoryHelper
3
3
 
4
4
  class << self
5
5
 
6
- def between(from, to)
6
+ def between(from=::Date.today-10, to=::Date.today+10)
7
7
  from = get_date_object(from)
8
8
  to = get_date_object(to)
9
9
 
@@ -25,9 +25,9 @@ module FactoryHelper
25
25
  end
26
26
 
27
27
  def birthday(min_age = 18, max_age = 65)
28
- t = ::Date.today
29
- from = ::Date.new(t.year - min_age, t.month, t.day - 1)
30
- to = ::Date.new(t.year - max_age, t.month, t.day + 1)
28
+ today= ::Date.today
29
+ from = ::Date.new(today.year - min_age, today.month, today.day) - 1
30
+ to = ::Date.new(today.year - max_age, today.month, today.day) + 1
31
31
 
32
32
  between(from, to).to_date
33
33
  end
@@ -5,7 +5,7 @@ module FactoryHelper
5
5
  class << self
6
6
  def credit_card(*types)
7
7
  types = CREDIT_CARD_TYPES if types.empty?
8
- type = types.sample
8
+ type = types.sample(:random => FactoryHelper::Config.random)
9
9
  template = numerify(fetch("credit_card.#{type}"))
10
10
 
11
11
  # calculate the luhn checksum digit
@@ -2,18 +2,18 @@
2
2
  module FactoryHelper
3
3
  class Hacker < Base
4
4
  flexible :hacker
5
-
5
+
6
6
  class << self
7
7
  def say_something_smart
8
- phrases.sample
8
+ phrases.sample(:random => FactoryHelper::Config.random)
9
9
  end
10
-
10
+
11
11
  def abbreviation; fetch('hacker.abbreviation'); end
12
12
  def adjective; fetch('hacker.adjective'); end
13
13
  def noun; fetch('hacker.noun'); end
14
14
  def verb; fetch('hacker.verb'); end
15
15
  def ingverb; fetch('hacker.ingverb'); end
16
-
16
+
17
17
  def phrases
18
18
  [ "If we #{verb} the #{noun}, we can get to the #{abbreviation} #{noun} through the #{adjective} #{abbreviation} #{noun}!",
19
19
  "We need to #{verb} the #{adjective} #{abbreviation} #{noun}!",
@@ -22,7 +22,7 @@ module FactoryHelper
22
22
  "Use the #{adjective} #{abbreviation} #{noun}, then you can #{verb} the #{adjective} #{noun}!",
23
23
  "The #{abbreviation} #{noun} is down, #{verb} the #{adjective} #{noun} so we can #{verb} the #{abbreviation} #{noun}!",
24
24
  "#{ingverb} the #{noun} won't do anything, we need to #{verb} the #{adjective} #{abbreviation} #{noun}!",
25
- "I'll #{verb} the #{adjective} #{abbreviation} #{noun}, that should #{noun} the #{abbreviation} #{noun}!"
25
+ "I'll #{verb} the #{adjective} #{abbreviation} #{noun}, that should #{noun} the #{abbreviation} #{noun}!"
26
26
  ]
27
27
  end
28
28
  end
@@ -11,12 +11,19 @@ module FactoryHelper
11
11
  end
12
12
 
13
13
  def safe_email(name = nil)
14
- [user_name(name), 'example.'+ %w[org com net].shuffle.first].join('@')
14
+ [
15
+ user_name(name),
16
+ 'example.'+ %w[org com net].
17
+ shuffle(:random => FactoryHelper::Config.random).first
18
+ ].join('@')
15
19
  end
16
20
 
17
21
  def user_name(specifier = nil, separators = %w(. _))
18
22
  if specifier.kind_of? ::String
19
- return specifier.scan(/\w+/).shuffle.join(separators.sample).downcase
23
+ return specifier.scan(/\w+/).
24
+ shuffle(:random => FactoryHelper::Config.random).
25
+ join(separators.sample(:random => FactoryHelper::Config.random)).
26
+ downcase
20
27
  elsif specifier.kind_of? Integer
21
28
  tries = 0 # Don't try forever in case we get something like 1_000_000.
22
29
  begin
@@ -41,15 +48,15 @@ module FactoryHelper
41
48
  Proc.new {
42
49
  [ Name.first_name, Name.last_name ].map {|n|
43
50
  n.gsub(/\W/, '')
44
- }.join(separators.sample).downcase }
45
- ].sample.call)
51
+ }.join(separators.sample(:random => FactoryHelper::Config.random)).downcase }
52
+ ].sample(:random => FactoryHelper::Config.random).call)
46
53
  end
47
54
 
48
55
  def password(min_length = 8, max_length = 16)
49
56
  temp = Lorem.characters(min_length)
50
57
  diff_length = max_length - min_length
51
58
  if diff_length > 0
52
- diff_rand = rand(diff_length + 1)
59
+ diff_rand = FactoryHelper::Config.random.rand(diff_length + 1)
53
60
  temp += Lorem.characters(diff_rand)
54
61
  end
55
62
  temp = temp[0..min_length] if min_length > 0
@@ -60,17 +67,6 @@ module FactoryHelper
60
67
  [ fix_umlauts(domain_word), domain_suffix ].join('.')
61
68
  end
62
69
 
63
- def fix_umlauts(string)
64
- string.gsub(/[äöüß]/i) do |match|
65
- case match.downcase
66
- when "ä" 'ae'
67
- when "ö" 'oe'
68
- when "ü" 'ue'
69
- when "ß" 'ss'
70
- end
71
- end
72
- end
73
-
74
70
  def domain_word
75
71
  Company.name.split(' ').first.gsub(/\W/, '').downcase
76
72
  end
@@ -81,21 +77,25 @@ module FactoryHelper
81
77
 
82
78
  def mac_address(prefix='')
83
79
  prefix_digits = prefix.split(':').map{ |d| d.to_i(16) }
84
- address_digits = (6 - prefix_digits.size).times.map{ rand(256) }
80
+ address_digits = (6 - prefix_digits.size).times.map do
81
+ FactoryHelper::Config.random.rand(256)
82
+ end
85
83
  (prefix_digits + address_digits).map{ |d| '%02x' % d }.join(':')
86
84
  end
87
85
 
88
86
  def ip_v4_address
89
87
  ary = (2..254).to_a
90
- [ary.sample,
91
- ary.sample,
92
- ary.sample,
93
- ary.sample].join('.')
88
+ [ary.sample(:random => FactoryHelper::Config.random),
89
+ ary.sample(:random => FactoryHelper::Config.random),
90
+ ary.sample(:random => FactoryHelper::Config.random),
91
+ ary.sample(:random => FactoryHelper::Config.random)].join('.')
94
92
  end
95
93
 
96
94
  def ip_v6_address
97
95
  @@ip_v6_space ||= (0..65535).to_a
98
- container = (1..8).map{ |_| @@ip_v6_space.sample }
96
+ container = (1..8).map do |_|
97
+ @@ip_v6_space.sample(:random => FactoryHelper::Config.random)
98
+ end
99
99
  container.map{ |n| n.to_s(16) }.join(':')
100
100
  end
101
101
 
@@ -104,13 +104,23 @@ module FactoryHelper
104
104
  end
105
105
 
106
106
  def slug(words = nil, glue = nil)
107
- glue ||= %w[- _ .].sample
107
+ glue ||= %w[- _ .].sample(:random => FactoryHelper::Config.random)
108
108
  (words || FactoryHelper::Lorem::words(2).join(' ')).gsub(' ', glue).downcase
109
109
  end
110
110
 
111
111
  def device_token
112
- rand(16 ** 64).to_s(16).rjust(64, '0').chars.to_a.shuffle.join
112
+ FactoryHelper::Config.random.rand(16 ** 64).to_s(16).rjust(64, '0').
113
+ chars.to_a.shuffle(:random => FactoryHelper::Config.random).join
113
114
  end
115
+
116
+ def fix_umlauts(string= '')
117
+ {"ä" => 'ae', "ö" => 'oe', "ü" => 'ue', "ß" => 'ss'}.each do |key, val|
118
+ string.gsub!(key.downcase, val.downcase)
119
+ string.gsub!(key.upcase, val.upcase)
120
+ end
121
+ string
122
+ end
123
+
114
124
  end
115
125
  end
116
126
  end
@@ -3,7 +3,7 @@ module FactoryHelper
3
3
  class Lorem < Base
4
4
  class << self
5
5
  def word
6
- translate('factory_helper.lorem.words').sample
6
+ translate('factory_helper.lorem.words').sample(:random => FactoryHelper::Config.random)
7
7
  end
8
8
 
9
9
  def words(num = 3, supplemental = false)
@@ -13,21 +13,23 @@ module FactoryHelper
13
13
  (supplemental ? translate('factory_helper.lorem.supplemental') : [])
14
14
  )
15
15
  word_list = word_list * ((resolved_num / word_list.length) + 1)
16
- word_list.shuffle[0, resolved_num]
16
+ word_list.shuffle(:random => FactoryHelper::Config.random)[0, resolved_num]
17
17
  end
18
18
 
19
19
  def character
20
20
  characters(1)
21
21
  end
22
22
 
23
- def characters(char_count = 255)
23
+ def characters(char_count= 255)
24
24
  return '' if char_count.respond_to?(:to_i) && char_count.to_i < 1
25
25
  char_count = resolve(char_count)
26
- rand(36**char_count).to_s(36).rjust(char_count, '0').chars.to_a.shuffle.join
26
+ FactoryHelper::Config.random.rand(36**char_count).to_s(36).
27
+ rjust(char_count, '0').chars.to_a.
28
+ shuffle(:random => FactoryHelper::Config.random).join
27
29
  end
28
30
 
29
31
  def sentence(word_count = 4, supplemental = false, random_words_to_add = 6)
30
- words(word_count + rand(random_words_to_add.to_i).to_i, supplemental).join(' ').capitalize + '.'
32
+ words(word_count + FactoryHelper::Config.random.rand(random_words_to_add.to_i).to_i, supplemental).join(' ').capitalize + '.'
31
33
  end
32
34
 
33
35
  def sentences(sentence_count = 3, supplemental = false)
@@ -39,7 +41,7 @@ module FactoryHelper
39
41
  end
40
42
 
41
43
  def paragraph(sentence_count = 3, supplemental = false, random_sentences_to_add = 3)
42
- sentences(resolve(sentence_count) + rand(random_sentences_to_add.to_i).to_i, supplemental).join(' ')
44
+ sentences(resolve(sentence_count) + FactoryHelper::Config.random.rand(random_sentences_to_add.to_i).to_i, supplemental).join(' ')
43
45
  end
44
46
 
45
47
  def paragraphs(paragraph_count = 3, supplemental = false)
@@ -56,8 +58,8 @@ module FactoryHelper
56
58
  # All other values are simply returned.
57
59
  def resolve(value)
58
60
  case value
59
- when Array then value[rand(value.size)]
60
- when Range then rand((value.last+1) - value.first) + value.first
61
+ when Array then value[FactoryHelper::Config.random.rand(value.size)]
62
+ when Range then FactoryHelper::Config.random.rand((value.last+1) - value.first) + value.first
61
63
  else value
62
64
  end
63
65
  end
@@ -1,23 +1,23 @@
1
1
  module FactoryHelper
2
2
  class Number < Base
3
3
  class << self
4
- def number(digits)
4
+ def number(digits=10)
5
5
  (1..digits).collect {digit}.join
6
6
  end
7
7
 
8
- def decimal(l_digits, r_digits = 2)
8
+ def decimal(l_digits=5, r_digits = 2)
9
9
  l_d = self.number(l_digits)
10
10
  r_d = self.number(r_digits)
11
11
  "#{l_d}.#{r_d}"
12
12
  end
13
13
 
14
14
  def digit
15
- (rand() * 9).round.to_s
15
+ (FactoryHelper::Config.random.rand() * 9).round.to_s
16
16
  end
17
17
 
18
- def hexadecimal(digits)
18
+ def hexadecimal(digits=6)
19
19
  hex = ""
20
- digits.times { hex += rand(15).to_s(16) }
20
+ digits.times { hex += FactoryHelper::Config.random.rand(15).to_s(16) }
21
21
  hex
22
22
  end
23
23
 
@@ -40,7 +40,7 @@ module FactoryHelper
40
40
  # Since extensions can be of variable length, this method taks a length parameter
41
41
  def subscriber_number(length = 4)
42
42
  begin
43
- rand.to_s[2..(1 + length)]
43
+ FactoryHelper::Config.random.rand.to_s[2..(1 + length)]
44
44
  rescue I18n::MissingTranslationData
45
45
  nil
46
46
  end
@@ -1,28 +1,32 @@
1
1
  module FactoryHelper
2
2
  class String
3
- def self.random(length= 32)
4
- if length.class.included_modules.include? Enumerable
5
- utf8string length.to_enum.to_a.sample
6
- else
7
- utf8string length
3
+ class << self
4
+ def random(length= 32)
5
+ utf8string select_a length
8
6
  end
9
- end
10
7
 
11
- protected
8
+ private
12
9
 
13
- def self.utf8string(length)
14
- (1..length.to_i).map do |_|
15
- characters.sample.chr(Encoding::UTF_8)
16
- end.join.force_encoding("UTF-8")
17
- end
10
+ def select_a length
11
+ if length.class.included_modules.include? Enumerable
12
+ select_a length.to_enum.to_a.sample(:random => FactoryHelper::Config.random)
13
+ else
14
+ length
15
+ end
16
+ end
18
17
 
19
- def self.characters
20
- [
21
- rand(59)+ 32, # latin alphabet
22
- rand(128), # 7-bit ASCII
23
- rand(0xd800), # utf-8 codepoints below utf-16 surrogate halves
24
- rand(0x102000)+ 0xe000 # utf-8 codepoints above utf-16 surrogate halves
25
- ]
18
+ def utf8string(length)
19
+ (1..length.to_i).map{ utf8character }.join
20
+ end
21
+
22
+ def utf8character
23
+ [
24
+ FactoryHelper::Config.random.rand(59)+ 32, # latin alphabet
25
+ FactoryHelper::Config.random.rand(128), # 7-bit ASCII
26
+ FactoryHelper::Config.random.rand(0xd800), # utf-8 codepoints below utf-16 surrogate halves
27
+ FactoryHelper::Config.random.rand(0x102000)+ 0xe000 # utf-8 codepoints above utf-16 surrogate halves
28
+ ].sample(:random => FactoryHelper::Config.random).chr(Encoding::UTF_8)
29
+ end
26
30
  end
27
31
  end
28
32
  end
@@ -12,7 +12,7 @@ module FactoryHelper
12
12
  end
13
13
 
14
14
  def state
15
- fetch('factory_helper.address.state').titleize
15
+ fetch('address.state').split(' ').map(&:capitalize).join(' ')
16
16
  end
17
17
  end
18
18
 
@@ -11,7 +11,8 @@ module FactoryHelper
11
11
  }
12
12
 
13
13
  class << self
14
- def between(from, to, period = :all)
14
+
15
+ def between(from=::Time.at(0), to=::Time.now, period = :all)
15
16
  super(from, to).to_time + random_time(period)
16
17
  end
17
18
 
@@ -31,7 +32,7 @@ module FactoryHelper
31
32
 
32
33
  def hours(period)
33
34
  raise ArgumentError, 'invalid period' unless TIME_RANGES.has_key? period
34
- hour_at_period = TIME_RANGES[period].to_a.sample
35
+ hour_at_period = TIME_RANGES[period].to_a.sample(:random => FactoryHelper::Config.random)
35
36
 
36
37
  (60 * 60 * hour_at_period)
37
38
  end
@@ -41,7 +42,7 @@ module FactoryHelper
41
42
  end
42
43
 
43
44
  def seconds
44
- (0..59).to_a.sample
45
+ (0..59).to_a.sample(:random => FactoryHelper::Config.random)
45
46
  end
46
47
  end
47
48
  end
@@ -1,3 +1,3 @@
1
1
  module FactoryHelper #:nodoc:
2
- VERSION = "1.6.7"
2
+ VERSION = "1.7.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factory-helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.7
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Giaraffa
@@ -9,93 +9,93 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-05-29 00:00:00.000000000 Z
12
+ date: 2015-06-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - "~>"
18
+ - - ~>
19
19
  - !ruby/object:Gem::Version
20
20
  version: '1.7'
21
21
  type: :development
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - "~>"
25
+ - - ~>
26
26
  - !ruby/object:Gem::Version
27
27
  version: '1.7'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: rake
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - "~>"
32
+ - - ~>
33
33
  - !ruby/object:Gem::Version
34
34
  version: '10.0'
35
35
  type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - "~>"
39
+ - - ~>
40
40
  - !ruby/object:Gem::Version
41
41
  version: '10.0'
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: pry
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - "~>"
46
+ - - ~>
47
47
  - !ruby/object:Gem::Version
48
48
  version: '0.10'
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - "~>"
53
+ - - ~>
54
54
  - !ruby/object:Gem::Version
55
55
  version: '0.10'
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: coveralls
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - "~>"
60
+ - - ~>
61
61
  - !ruby/object:Gem::Version
62
62
  version: '0.8'
63
63
  type: :development
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
- - - "~>"
67
+ - - ~>
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0.8'
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: rspec
72
72
  requirement: !ruby/object:Gem::Requirement
73
73
  requirements:
74
- - - "~>"
74
+ - - ~>
75
75
  - !ruby/object:Gem::Version
76
76
  version: '3.2'
77
77
  type: :development
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
- - - "~>"
81
+ - - ~>
82
82
  - !ruby/object:Gem::Version
83
83
  version: '3.2'
84
84
  - !ruby/object:Gem::Dependency
85
85
  name: i18n
86
86
  requirement: !ruby/object:Gem::Requirement
87
87
  requirements:
88
- - - "~>"
88
+ - - ~>
89
89
  - !ruby/object:Gem::Version
90
90
  version: '0.7'
91
91
  type: :runtime
92
92
  prerelease: false
93
93
  version_requirements: !ruby/object:Gem::Requirement
94
94
  requirements:
95
- - - "~>"
95
+ - - ~>
96
96
  - !ruby/object:Gem::Version
97
97
  version: '0.7'
98
- description: 'Easily generate fake data: names, addresses, phone numbers, etc.'
98
+ description: ! 'Easily generate fake data: names, addresses, phone numbers, etc.'
99
99
  email:
100
100
  - factory-helper@googlegroups.com
101
101
  executables: []
@@ -165,17 +165,17 @@ require_paths:
165
165
  - lib
166
166
  required_ruby_version: !ruby/object:Gem::Requirement
167
167
  requirements:
168
- - - ">="
168
+ - - ! '>='
169
169
  - !ruby/object:Gem::Version
170
170
  version: 1.9.3
171
171
  required_rubygems_version: !ruby/object:Gem::Requirement
172
172
  requirements:
173
- - - ">="
173
+ - - ! '>='
174
174
  - !ruby/object:Gem::Version
175
175
  version: '0'
176
176
  requirements: []
177
177
  rubyforge_project:
178
- rubygems_version: 2.4.4
178
+ rubygems_version: 2.4.3
179
179
  signing_key:
180
180
  specification_version: 4
181
181
  summary: Easily generate fake data