Module: ZFaker

Defined in:
lib/zfaker.rb

Overview

ZFaker Module

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_const(file) ⇒ Object



66
67
68
69
70
# File 'lib/zfaker.rb', line 66

def self.add_const(file)
  extn = File.extname  file
  name = File.basename file, extn  
  build_const(name, file)
end

.build_const(const_name, file) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/zfaker.rb', line 33

def self.build_const(const_name, file)
  data_path = file
  reader = k File.read(data_path).split("\n")
  arrout = []
  reader.each do |x|
    cut = x.split(' ')
    if cut.size > 1 && cut[0].integer?
      weight = cut[0].to_i
      element = cut[1..-1].join(' ')
      element = '' if element == '<blank>'
    else
      weight = 1
      element = cut[0..-1].join(' ')
    end
    weight.times { arrout.push(element) }
  end
  const_set const_name.upcase, arrout
  arrout
end

.const_array(argument) ⇒ Object



25
26
27
28
# File 'lib/zfaker.rb', line 25

def self.const_array(argument)
  array = argument.is_a?(Array) ? argument : argument.to_a
  freeze_all(array)
end

.freeze_all(array) ⇒ Object



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

def self.freeze_all(array)
  array.each(&:freeze)
  array.freeze
  array
end

.k(arg) ⇒ Object



30
31
# File 'lib/zfaker.rb', line 30

def self.k(arg) const_array(arg)
end

Instance Method Details

#unique_sample(arr) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/zfaker.rb', line 53

def unique_sample(arr)
  sample = ''
  collisions = 0
  loop do
    sample = yield
    break unless arr.include? sample
    collisions += 1
    fail 'Too Many Collisions: ' + collisions.to_s if collisions > (arr.size + 50) * 10
  end
  arr.push sample
  sample
end