class ToFactory::KlassInference

Public Class Methods

new(representations) click to toggle source
# File lib/to_factory/klass_inference.rb, line 9
def initialize(representations)
  @mapping = {}

  representations.each do |r|
    set_mapping_from(r)
  end
end

Public Instance Methods

infer(factory_name, count=0) click to toggle source
# File lib/to_factory/klass_inference.rb, line 17
def infer(factory_name, count=0)
  count = count + 1
  result  = @mapping[factory_name]
  return [result, count] if result.is_a? Class

  if result.nil?
    raise CannotInferClass.new(factory_name)
  end

  infer(result, count)
end

Private Instance Methods

set_mapping_from(r) click to toggle source
# File lib/to_factory/klass_inference.rb, line 31
def set_mapping_from(r)
  if parent_klass = to_const(r.parent_name)
    @mapping[r.parent_name] = parent_klass
  end

  @mapping[r.name] =
    if factory_klass = to_const(r.name)
      factory_klass
    else
      r.parent_name
    end
end
to_const(factory_name) click to toggle source
# File lib/to_factory/klass_inference.rb, line 44
def to_const(factory_name)
  factory_name.to_s.camelize.constantize
rescue NameError
  nil
end