class ToFactory::Finders::Model

Public Instance Methods

call(exclusions: [], klasses: nil) click to toggle source
# File lib/to_factory/finders/model.rb, line 4
def call(exclusions: [], klasses: nil)
  instances = []
  klasses ||= find_klasses(exclusions)

  klasses.each do |klass|
    if instance = get_active_record_instance(klass)
      instances << instance
    end
  end

  instances
end

Private Instance Methods

find_klasses(exclusions) click to toggle source
# File lib/to_factory/finders/model.rb, line 19
def find_klasses(exclusions)
  klasses = []
  models.each do |file|
    matching_lines(file) do |match|
      klass = rescuing_require(file, match)

      klasses << klass unless exclusions.include?(klass) 
    end
  end

  klasses
end
get_active_record_instance(klass) click to toggle source
# File lib/to_factory/finders/model.rb, line 54
def get_active_record_instance(klass)
  if klass && klass.ancestors.include?(ActiveRecord::Base)
    klass.first
  end
rescue StandardError => e
  warn "Failed to get record from #{klass} #{e.message.inspect}"
end
matching_lines(file) { |match| ... } click to toggle source
# File lib/to_factory/finders/model.rb, line 36
def matching_lines(file)
  File.readlines(file).each do |l|
    if match = l.match(/class (.*) ?</)
      yield match
    end
  end
end
models() click to toggle source
# File lib/to_factory/finders/model.rb, line 32
def models
  Dir.glob("#{ToFactory.models}/**/*.rb")
end
rescuing_require(file, match) click to toggle source
# File lib/to_factory/finders/model.rb, line 44
def rescuing_require(file, match)
  require file
  klass = eval(match[1])
  klass

rescue Exception => e
  warn "Failed to eval #{file}"
  warn e.message
end