Method: QuickFaker#lookup

Defined in:
lib/quick_faker.rb

#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