Class: QuickFaker
- Inherits:
-
Object
show all
- Defined in:
- lib/quick_faker.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(locale = 'en-GB', debug: false) ⇒ QuickFaker
Returns a new instance of QuickFaker.
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/quick_faker.rb', line 18
def initialize(locale='en-GB', debug: false)
@debug = debug
Faker::Config.locale = locale
s = File.join(File.dirname(__FILE__), '..', 'data', 'faker.yaml')
a = YAML.load(File.read(s))
@a = a.flat_map(&:last)
load_methods(a)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
109
110
111
|
# File 'lib/quick_faker.rb', line 109
def method_missing(method_name, *args)
lookup(method_name, *args)
end
|
Instance Attribute Details
#a ⇒ Object
Returns the value of attribute a.
16
17
18
|
# File 'lib/quick_faker.rb', line 16
def a
@a
end
|
Instance Method Details
#lookup(s, context = nil) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/quick_faker.rb', line 31
def lookup(s, context=nil)
found = @h[s.to_sym]
puts 'found: ' + found.inspect if @debug
if found and found[0].is_a? Array
h = found.map {|x| x.last[/[^:]+(?=\.)/].downcase.to_sym}.zip(found.map(&:first)).to_h
if context then
h[context.to_sym].call
elsif h.keys.include? :name
h[:name].call
else
raise 'provide context! options: ' + h.keys.map(&:to_s).join(', ') \
+ "\n try e.g. lookup(:#{s}, :#{h.keys.first})"
end
elsif found
found[0].call
else
puts ('*' + s.to_s + '* not found').warning
puts ('try: ').info
@a.grep /#{s}/i
end
end
|
#lookup2(s) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/quick_faker.rb', line 56
def lookup2(s)
found = @h[s.to_sym]
if found and found[0].is_a? Array then
found.map(&:last)
elsif found[0]
found.last
else
puts ('*' + s.to_s + '* not found').warning
puts ('try: ').info
@a.grep(/#{s}/i)[/[^\.]+$/].uniq.sort
end
end
|
#to_h ⇒ Object
71
72
73
|
# File 'lib/quick_faker.rb', line 71
def to_h()
@h
end
|