-
Notifications
You must be signed in to change notification settings - Fork 556
Expand file tree
/
Copy pathreference.rb
More file actions
93 lines (74 loc) · 2.27 KB
/
Copy pathreference.rb
File metadata and controls
93 lines (74 loc) · 2.27 KB
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
# frozen_string_literal: true
require_relative '../lib/ffaker'
# always use the same seed for consistency
FFaker::Random.seed = 1337
ICONS = {
error: '‼️',
warning: '❗'
}.freeze
UTILS_MODULES = %i[ArrayUtils ModuleUtils RandomUtils Random Version].freeze
UTILS_METHODS = %i[k underscore fetch_sample rand shuffle unique luhn_check].freeze
# Get a list of sections
def faker_modules
FFaker
.constants
.reject { |const| UTILS_MODULES.include?(const) }
.select { |const| FFaker.const_get(const).instance_of?(Module) }
.sort
.map { |const| FFaker.const_get(const) }
end
# Returns faker methods for a given module
def faker_methods(mod)
methods = mod.methods - Module.methods - UTILS_METHODS
# For Company.name (et al), don't discard :name if it was reimplemented
methods << :name if mod.send(:name) != mod.to_s
methods
end
# Catch deprecation warnings.
# This `#warn` overrides Kernel#warn
def warn(msg)
return unless Kernel.instance_variable_get(:@ffaker_warnings)
Kernel.instance_variable_set(:@ffaker_warnings, Kernel.instance_variable_get(:@ffaker_warnings) << msg)
end
def catch_warnings
Kernel.instance_variable_set(:@ffaker_warnings, [])
[yield, Kernel.instance_variable_get(:@ffaker_warnings)]
end
def escape(str)
str.gsub('&', '&').gsub('<', '<').gsub('>', '>').delete("\n")
end
sections = faker_modules.map do |mod|
lines = []
methods = faker_methods(mod)
lines << "## #{mod}"
lines << ''
lines << '| Method | Example |'
lines << '| ------ | ------- |'
methods.sort.each do |meth|
arity = mod.method(meth).arity
left = "`#{meth}`"
right = ''
if arity.positive?
left = "`#{meth}`(#{Array.new(arity) { '...' }.join(', ')})"
else
begin
examples, warnings = catch_warnings do
Array.new(3) { mod.unique.send meth }
end
right = warnings.any? ? "#{ICONS[:warning]} *#{warnings.first}*" : (escape examples.join(', ')).to_s
rescue StandardError => e
right = "#{ICONS[:error]} #{e.class}: #{e.message}"
end
end
lines << "| #{left} | #{right} |"
end
lines << ''
end
puts '# FFaker reference'
puts ''
faker_modules.each do |mod|
name = mod.to_s.downcase.delete(':')
puts " * [#{mod}](##{name})"
end
puts ''
puts sections.flatten.join("\n")