Sha256: bcc6bb429ca09621336ff94c5c67ebf40da02215cc3127a3e2d95eea99e646ef

Contents?: true

Size: 729 Bytes

Versions: 14

Compression:

Stored size: 729 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 compute_type_without_fallback(obj_class)
    if obj_class = load_class(obj_class)
      obj_class
    else
      raise ObjClassNotFound
    end
  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

14 entries across 14 versions & 1 rubygems

Version Path
scrivito_sdk-1.0.0 lib/scrivito/type_computer.rb
scrivito_sdk-1.0.0.rc4 lib/scrivito/type_computer.rb
scrivito_sdk-1.0.0.rc3 lib/scrivito/type_computer.rb
scrivito_sdk-1.0.0.rc2 lib/scrivito/type_computer.rb
scrivito_sdk-1.0.0.rc1 lib/scrivito/type_computer.rb
scrivito_sdk-0.90.0 lib/scrivito/type_computer.rb
scrivito_sdk-0.90.0.rc3 lib/scrivito/type_computer.rb
scrivito_sdk-0.90.0.rc2 lib/scrivito/type_computer.rb
scrivito_sdk-0.90.0.rc1 lib/scrivito/type_computer.rb
scrivito_sdk-0.71.2 lib/scrivito/type_computer.rb
scrivito_sdk-0.71.1 lib/scrivito/type_computer.rb
scrivito_sdk-0.71.0 lib/scrivito/type_computer.rb
scrivito_sdk-0.71.0.rc2 lib/scrivito/type_computer.rb
scrivito_sdk-0.71.0.rc1 lib/scrivito/type_computer.rb