Sha256: 051840fa5f78c61cacd5fefdcfbf63812cf216f595c1aa9177a7b869e46c507b

Contents?: true

Size: 573 Bytes

Versions: 3

Compression:

Stored size: 573 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)
    load_class(obj_class) || @fallback_class
  end

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

  private

  def load_class(obj_class)
    klass = obj_class.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.40.0.rc1 lib/scrivito/type_computer.rb
scrivito_sdk-0.30.0 lib/scrivito/type_computer.rb
scrivito_sdk-0.30.0.rc1 lib/scrivito/type_computer.rb