class FakerBot::Reflector

Exposes `Faker` reflection methods @api private

Attributes

descendants_with_methods[R]
query[R]

Public Class Methods

descendants() click to toggle source

Select `Faker` subclasses @return [Array] `Faker::Base` sub classes

# File lib/fakerbot/reflector.rb, line 12
def self.descendants
  @descendants ||= ObjectSpace.each_object(Class).select do |klass|
    klass < self
  end
end
find(query) click to toggle source
# File lib/fakerbot/reflector.rb, line 33
def find(query)
  new(query).find
end
list(show_methods: false) click to toggle source
# File lib/fakerbot/reflector.rb, line 37
def list(show_methods: false)
  new.list(show_methods)
end
my_singleton_methods() click to toggle source

Select public class methods @return [Array] public methods

# File lib/fakerbot/reflector.rb, line 20
def self.my_singleton_methods
  singleton_methods(false).select { |m| respond_to?(m) }
end
new(query = nil) click to toggle source
# File lib/fakerbot/reflector.rb, line 27
def initialize(query = nil)
  @descendants_with_methods = Hash.new { |h, k| h[k] = [] }
  @query = query
end

Public Instance Methods

find() click to toggle source
# File lib/fakerbot/reflector.rb, line 42
def find
  search_descendants_matching_query
  descendants_with_methods
end
list(show_methods) click to toggle source
# File lib/fakerbot/reflector.rb, line 47
def list(show_methods)
  show_methods ? all_descendants_with_methods : faker_descendants
end

Private Instance Methods

all_descendants_with_methods() click to toggle source
# File lib/fakerbot/reflector.rb, line 53
def all_descendants_with_methods
  faker_descendants.each do |faker|
    store(faker, faker.my_singleton_methods)
  end
  descendants_with_methods
end
faker_descendants() click to toggle source
# File lib/fakerbot/reflector.rb, line 78
def faker_descendants
  @faker_descendants ||= Faker::Base.descendants
end
query_matches?(method_name) click to toggle source
# File lib/fakerbot/reflector.rb, line 68
def query_matches?(method_name)
  method_name_parts = method_name.split(/_/).reject(&:empty?)
  query.match(/#{method_name_parts.join('|')}/)
end
search_descendants_matching_query() click to toggle source
# File lib/fakerbot/reflector.rb, line 60
def search_descendants_matching_query
  faker_descendants.each do |faker|
    methods = faker.my_singleton_methods
    matching = methods.select { |m| query_matches?(m.to_s) }
    store(faker, matching)
  end
end
store(descendant, methods) click to toggle source
# File lib/fakerbot/reflector.rb, line 73
def store(descendant, methods)
  return if methods.empty?
  descendants_with_methods[descendant].concat(methods)
end