Module: FFaker::ModuleUtils

Includes:
RandomUtils
Included in:
FFaker, AWS, Address, AddressAU, AddressBR, AddressCA, AddressCH, AddressCHDE, AddressCHFR, AddressCHIT, AddressDA, AddressDE, AddressFI, AddressFR, AddressGR, AddressID, AddressIN, AddressIT, AddressJA, AddressKR, AddressMX, AddressNL, AddressPL, AddressRU, AddressSE, AddressSN, AddressUA, AddressUK, AddressUS, Airline, Animal, AnimalBR, AnimalCN, AnimalES, AnimalPL, AnimalUS, Avatar, BaconIpsum, Bank, BankUS, Book, Boolean, CheesyLingo, Code, Color, ColorPL, ColorUA, Company, CompanyCN, CompanyFR, CompanyIT, CompanyJA, CompanySE, Conference, CoursesFR::Mathematiques, CoursesFR::Philosophie, Crypto, Currency, Date, DizzleIpsum, Education, EducationCN, Filesystem, Food, FoodPL, FreedomIpsum, Game, Gender, GenderBR, GenderCN, GenderID, GenderIT, GenderJA, GenderJP, GenderKR, GenderPL, GenderRU, Geolocation, Guid, HTMLIpsum, HealthcareIpsum, HealthcareRU, HipsterIpsum, Identification, IdentificationBR, IdentificationEC, IdentificationES, IdentificationESCL, IdentificationESCO, IdentificationIN, IdentificationIT, IdentificationKR, IdentificationMX, IdentificationPL, IdentificationTW, Image, Internet, InternetSE, JoJo, Job, JobBR, JobCN, JobFR, JobIT, JobJA, JobKR, JobVN, Locale, Lorem, LoremAR, LoremBR, LoremCN, LoremFR, LoremIE, LoremIT, LoremJA, LoremKR, LoremPL, LoremRU, LoremUA, Movie, Music, Name, NameAR, NameBR, NameCN, NameCS, NameDA, NameDE, NameES, NameFR, NameGA, NameGR, NameID, NameIN, NameIT, NameJA, NameKH, NameKR, NameMX, NameNB, NameNL, NamePH, NamePL, NameRU, NameSE, NameSN, NameTH, NameTHEN, NameTW, NameUA, NameVN, NatoAlphabet, Number, PhoneNumber, PhoneNumberAU, PhoneNumberBR, PhoneNumberCH, PhoneNumberCU, PhoneNumberDA, PhoneNumberDE, PhoneNumberFR, PhoneNumberID, PhoneNumberIT, PhoneNumberJA, PhoneNumberKR, PhoneNumberMX, PhoneNumberNL, PhoneNumberPL, PhoneNumberRU, PhoneNumberSE, PhoneNumberSG, PhoneNumberSN, PhoneNumberTW, PhoneNumberUA, Product, SSN, SSNMX, SSNSE, SemVer, Skill, Sport, SportPL, SportRU, SportUS, String, Time, Tweet, Unit, UnitEnglish, UnitMetric, Vehicle, Vehicle::VIN, Venue, Youtube
Defined in:
lib/ffaker/utils/module_utils.rb

Instance Method Summary collapse

Methods included from RandomUtils

#fetch_sample, #rand, #shuffle

Instance Method Details

#const_missing(const_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ffaker/utils/module_utils.rb', line 15

def const_missing(const_name)
  if const_name.match?(/[a-z]/) # Not a constant, probably a class/module name.
    super const_name
  else
    mod_name = ancestors.first.to_s.split('::').last
    data_path = "#{FFaker::BASE_LIB_PATH}/ffaker/data/#{underscore(mod_name)}/#{underscore(const_name.to_s)}"
    data = k File.read(data_path, mode: 'r:UTF-8').split("\n")
    const_set const_name, data
    data
  end
end

#k(arg) ⇒ Object



11
12
13
# File 'lib/ffaker/utils/module_utils.rb', line 11

def k(arg)
  FFaker::ArrayUtils.const_array(arg)
end

#luhn_check(number) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ffaker/utils/module_utils.rb', line 40

def luhn_check(number)
  multiplications = []

  number.chars.each_with_index do |digit, i|
    multiplications << i.even? ? digit.to_i * 2 : digit.to_i
  end

  sum = 0
  multiplications.each do |num|
    num.to_s.each_byte do |character|
      sum += character.chr.to_i
    end
  end

  control_digit = (sum % 10).zero? ? 0 : (((sum / 10) + 1) * 10) - sum
  control_digit.to_s
end

#underscore(string) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/ffaker/utils/module_utils.rb', line 27

def underscore(string)
  string.gsub('::', '/')
        .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
        .gsub(/([a-z\d])([A-Z])/, '\1_\2')
        .tr('-', '_')
        .downcase
end

#unique(max_retries = 10_000) ⇒ Object



35
36
37
# File 'lib/ffaker/utils/module_utils.rb', line 35

def unique(max_retries = 10_000)
  FFaker::UniqueUtils.add_instance(self, max_retries)
end