Sha256: 66db5edbaee75757d1556dadd820453d86ce331fa5216c14144d23868787e1f7

Contents?: true

Size: 702 Bytes

Versions: 3

Compression:

Stored size: 702 Bytes

Contents

module Scrivito

class TypeComputer
  def initialize(base_class, fallback_class)
    @base_class, @fallback_class = base_class, fallback_class
  end

  def compute_type(obj_class_name)
    compute_type_without_fallback(obj_class_name) || @fallback_class
  end

  def compute_type_without_fallback(obj_class_name)
    load_class(obj_class_name)
  end

  def special_class?(klass)
    klass == @base_class || klass == @fallback_class
  end

  private

  def load_class(obj_class_name)
    klass = obj_class_name.constantize
    klass if inherits_from_base?(klass)
  rescue NameError
  end

  def inherits_from_base?(klass)
    klass.ancestors.include?(@base_class) && klass != @base_class
  end
end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
scrivito_sdk-0.60.0 lib/scrivito/type_computer.rb
scrivito_sdk-0.60.0.rc2 lib/scrivito/type_computer.rb
scrivito_sdk-0.60.0.rc1 lib/scrivito/type_computer.rb