Method: NinjaModel::Base.compute_type

Defined in:
lib/ninja_model/base.rb

.compute_type(type_name) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ninja_model/base.rb', line 78

def compute_type(type_name)
  if type_name.match(/^::/)
    # If the type is prefixed with a scope operator then we assume that
    # the type_name is an absolute reference.
    ActiveSupport::Dependencies.constantize(type_name)
  else
    # Build a list of candidates to search for
    candidates = []
    name.scan(/::|$/) { candidates.unshift "#{$`}::#{type_name}" }
    candidates << type_name

    candidates.each do |candidate|
      begin
        constant = ActiveSupport::Dependencies.constantize(candidate)
        return constant if candidate == constant.to_s
      rescue NameError => e
        # We don't want to swallow NoMethodError < NameError errors
        raise e unless e.instance_of?(NameError)
      end
    end

    raise NameError, "uninitialized constant #{candidates.first}"
  end
end