File: ffaker.rb

package info (click to toggle)
ruby-ffaker 2.25.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,852 kB
  • sloc: ruby: 13,136; makefile: 8; sh: 1
file content (130 lines) | stat: -rw-r--r-- 2,831 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# frozen_string_literal: true

require_relative 'ffaker/version'
require 'pathname'

module FFaker
  require_relative 'ffaker/utils/array_utils'
  require_relative 'ffaker/utils/module_utils'

  extend ModuleUtils

  BASE_LIB_PATH = File.expand_path(__dir__)

  LETTERS = Array('a'..'z').freeze

  HEX = %w[0 1 2 3 4 5 6 7 8 9 A B C D E F].freeze

  def self.hexify(*masks)
    fetch_sample(masks.flatten).gsub('#') { fetch_sample(HEX) }
  end

  def self.numerify(*masks)
    fetch_sample(masks.flatten).gsub('#') { rand(0..9).to_s }
  end

  def self.letterify(*masks)
    fetch_sample(masks.flatten).gsub('?') { fetch_sample(LETTERS) }
  end

  def self.bothify(masks)
    letterify(numerify(masks))
  end

  autoload_acronyms = {
    'ar' => 'AR',
    'au' => 'AU',
    'aws' => 'AWS',
    'br' => 'BR',
    'ca' => 'CA',
    'ch' => 'CH',
    'cl' => 'CL',
    'cn' => 'CN',
    'co' => 'CO',
    'cs' => 'CS',
    'cu' => 'CU',
    'da' => 'DA',
    'de' => 'DE',
    'ec' => 'EC',
    'en' => 'EN',
    'es' => 'ES',
    'fi' => 'FI',
    'fr' => 'FR',
    'ga' => 'GA',
    'gr' => 'GR',
    'html' => 'HTML',
    'id' => 'ID',
    'ie' => 'IE',
    'in' => 'IN',
    'it' => 'IT',
    'ja' => 'JA',
    'jp' => 'JP',
    'kh' => 'KH',
    'kr' => 'KR',
    'mx' => 'MX',
    'nb' => 'NB',
    'nl' => 'NL',
    'ph' => 'PH',
    'pl' => 'PL',
    'ru' => 'RU',
    'se' => 'SE',
    'sg' => 'SG',
    'sn' => 'SN',
    'ssn' => 'SSN',
    'th' => 'TH',
    'tw' => 'TW',
    'ua' => 'UA',
    'uk' => 'UK',
    'us' => 'US',
    'uuid' => 'UUID',
    'vn' => 'VN'
  }

  directory = "#{__dir__}/ffaker"
  Dir["#{directory}/*.rb"].each do |file_name|
    relative_file_path = Pathname.new(file_name).relative_path_from(directory).to_s.chomp('.rb')
    ## Don't consider files in sub-directories
    constant_name = relative_file_path.split('_').map do |part|
      autoload_acronyms.fetch(part) { part.capitalize }
    end.join
    autoload constant_name, file_name
  end

  # Random Number Generator (RNG) used with ModuleUtils#fetch, #shuffle, #rand
  # in order to provide deterministic repeatability.
  module Random
    # Returns the current RNG seed.
    def self.seed
      @seed ||= ::Random.new_seed
    end

    # Sets the RNG seed and creates a new internal RNG.
    def self.seed=(new_seed)
      @seed = new_seed
      reset!
      new_seed
    end

    # Reset the RNG back to its initial state.
    def self.reset!
      @rng = new_rng
    end

    # Returns a random number using an RNG with a known seed.
    def self.rand(max = nil)
      return rng.rand(max) if max

      rng.rand
    end

    # Returns the current Random object.
    def self.rng
      @rng ||= new_rng
    end

    # Returns a new Random object instantiated with #seed.
    def self.new_rng
      ::Random.new(seed)
    end
  end
end