Sha256: c30897bd3ce59eaa4560e76bd3ad71b29d5761a4dcbef1ca8189799119120b6a

Contents?: true

Size: 759 Bytes

Versions: 14

Compression:

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

  def compute_type_without_fallback(obj_class_name)
    if obj_class = load_class(obj_class_name)
      obj_class
    else
      raise ObjClassNotFound
    end
  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

14 entries across 14 versions & 1 rubygems

Version Path
scrivito_sdk-0.70.2 lib/scrivito/type_computer.rb
scrivito_sdk-0.70.1 lib/scrivito/type_computer.rb
scrivito_sdk-0.70.0 lib/scrivito/type_computer.rb
scrivito_sdk-0.70.0.rc3 lib/scrivito/type_computer.rb
scrivito_sdk-0.70.0.rc2 lib/scrivito/type_computer.rb
scrivito_sdk-0.70.0.rc1 lib/scrivito/type_computer.rb
scrivito_sdk-0.66.0 lib/scrivito/type_computer.rb
scrivito_sdk-0.66.0.rc2 lib/scrivito/type_computer.rb
scrivito_sdk-0.65.2 lib/scrivito/type_computer.rb
scrivito_sdk-0.66.0.rc1 lib/scrivito/type_computer.rb
scrivito_sdk-0.65.1 lib/scrivito/type_computer.rb
scrivito_sdk-0.65.0 lib/scrivito/type_computer.rb
scrivito_sdk-0.65.0.rc2 lib/scrivito/type_computer.rb
scrivito_sdk-0.65.0.rc1 lib/scrivito/type_computer.rb