Module: Faker

Defined in:
lib/faker_extensions/faker.rb

Overview

Added by Mike Urban to enhance data population abilities provided by the faker module 10/2014

Class Method Summary collapse

Class Method Details

.coin_flipObject



4
5
6
# File 'lib/faker_extensions/faker.rb', line 4

def self.coin_flip
  [true,false].shuffle.first   #rand select for boolean, essentially a coin flip

end

.date_range(p_from, p_to) ⇒ Object



37
38
39
# File 'lib/faker_extensions/faker.rb', line 37

def self.date_range(p_from, p_to)
  (p_from..p_to).to_a.shuffle.first
end

.gender(p_male = 'male', p_female = 'female') ⇒ Object



29
30
31
# File 'lib/faker_extensions/faker.rb', line 29

def self.gender(p_male = 'male', p_female = 'female')
  l_gender = coin_flip ? p_male : p_female #genders need to be passed if different than english or you want boy/girl for example

end

.integer_range(p_from, p_to) ⇒ Object



33
34
35
# File 'lib/faker_extensions/faker.rb', line 33

def self.integer_range(p_from, p_to)
  (p_from..p_to).to_a.shuffle.first
end

.mostly_false(p_pct = 75) ⇒ Object



25
26
27
# File 'lib/faker_extensions/faker.rb', line 25

def self.mostly_false(p_pct=75)
  !mostly_true(p_pct)
end

.mostly_true(p_pct = 75) ⇒ Object



19
20
21
22
23
# File 'lib/faker_extensions/faker.rb', line 19

def self.mostly_true(p_pct=75)
  #no more than 99%, no less than 51% can be mostly true

  p_num_true = [[p_pct.to_i, 99].min, 51].max
  (1..100).to_a.shuffle.first <= p_num_true
end

.random_paragraphs(p_max_paragraphs, p_paragraph_separator, p_allow_nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/faker_extensions/faker.rb', line 8

def self.random_paragraphs(p_max_paragraphs, p_paragraph_separator, p_allow_nil)
  l_text = ''
  l_start = p_allow_nil ? 0 : 1
  l_num_paragraphs = (l_start..p_max_paragraphs).to_a.shuffle.first
  l_num_paragraphs.times do |ctr|
    l_text += "#{p_paragraph_separator}" if ctr > 0
    l_text += Faker::Lorem.paragraph
  end
  l_text.blank? ? nil : l_text
end